Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6173473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:34:58+00:00 2026-05-23T23:34:58+00:00

Goal: Best system for refactoring complex & ugly code in exactly the same file.

  • 0

Goal: Best system for refactoring complex & ugly code in exactly the same file.

I’m currently in the process of refactoring someone else’s bad code. The code is intermixed, procedural php and I want to be able to create templates -in- the script. I don’t want to have to create an external file of a different type to run the template, I want to be able to take the horrible stuff happening in a script and make it into a template in the script, and then call that template right where the horrible stuff once was. And still be able to see what the template is outputting in the same file.

I just recently rediscovered php’s alternative syntax to simplify native templating ( http://php.net/manual/en/control-structures.alternative-syntax.php ).

Example System

Are there other techniques for really simplifying the templating process with native php templates? I generally prefer another template engine because it makes the php echoing slightly simpler than using php syntax, but especially when refactoring other people’s code, sometimes a templating engine is a lot of overhead and imposes a lot of rules, such as having your templates in some “templates” folder seperate from the script you’re refactoring, which makes it difficult for placement when refactoring.

Specifically, I’m trying to embark on a project to create a “templating” system that relies on native php, with templates that can be inline in source php scripts. The reason that I’m doing this is because I’ve found that, at least in the initial stage, having to switch from template to calling script, back and forth, makes it slightly harder to deal with, and since I am refactoring really complex and ugly code, I want to be able to see everything that’s happening as easily as possible. So I’d like to do something like

// Inclusion of the template library
require_once('lib_template.php');
// Initialization Logic
...lots of ugly non-display code and data-obtaining code here

// Decide what will be able to be output and put it into an array to pass to the template
$template_output_variables = array('name'=>'no name');

// Define the template function here, has html and such in native php here.
$contact_us = function page_contact_us($template_output_variables=null){
// Leave php in the template function.
?>
<body> Hello World
</body>

<?php
}

// call & display the template via delayed execution, display it within a pre-set head and footer.
display_page($template=$contact_us, $template_output_stuff=$template_output_variables, $options=array('title'=>'Contact Us'));

Looking for beneficial techniques for inline “template” sections and clean native php templates

So, I’m wondering what other useful techniques there are out there for making cleaner native-php-template code and I’d also really love to know if someone else out there did a inline-in-file templating system/library/script in php, so I can check it out and use that to inform writing my own.

Here’s a github gist of what I have so far:
https://gist.github.com/1201969

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:34:58+00:00Added an answer on May 23, 2026 at 11:34 pm

    Output buffering is a handy tool when lacking a template engine.

    ob_start(); //start the buffer
    
    echo "some stuff";
    include ('sometemplatefile.php');
    //etc etc
    
    $body=ob_get_contents(); //get the junk out of the buffer
    ob_end_clean(); //clear the buffer
    
    //do search and replace on contents... 
    //etc
    
    include ('header.php');
    echo $body;
    include ('footer.php');
    

    Maybe something like heredoc syntax is what you were asking for:
    http://us.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

    Also, if you have short open tags enabled this can come in handy:

    Some HTML: 
    <?=$data?> 
    This is a shortcut for 
    <? echo $data;?>
    

    I think this may be deprecated in a future version though (I seem to remember reading somewhere)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A colleague and I are discussing best practices regarding ordering method parameters. The goal
My goal is to maintain a web file server separately from my main ASP.NET
Hmmm. Consider this program, whose goal is to figure out the best way to
What is the best way to retrieve a custom class name? My goal is
I'm building an Cocoa application that modifies a file on the user's operating system
Goal Java client for Yahoo's HotJobs Resumé Search REST API . Background I'm used
Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
My Goal I would like to have a main processing thread (non GUI), and
My goal here is to create a very simple template language. At the moment,
my goal is to write a stored proc that can collect all field values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.