How to make use of templates within a PHP application, without using a complex templating engine.

In small projects it’s easy to fall into the trap of quickly creating PHP interspersed with markup, writing code in logical sequence, but losing the separation between presentation and business logic.
No matter how small even for single page sites, it’s always a good idea to try separate the two. The most obvious practical reason is as the site expands in size and complexity and as the inevitable visual changes are requested by the client, it’s much quicker and easier to change a well laid out application with this logical separation of code and design. That is to say it’s a good idea for the benefit of extensibility and maintainability.
Although for a basic deployment it can often not warrant the time or bloat of a fully fledged templating engine, say a PEAR or Smarty solution.
Basic templating is still available using standard PHP functions and without the need for a complex templating engine. Allowing small application or website to adhere to the basics of MVC (Model View Controller) design pattern (mainly the V and C part of that). The following also provides an easy way to still use templates for emails as well as standard output for the browser.
Here’s how
To start with it’s a good idea to separate the files themselves, putting all the visual or view files into a separate folder by themselves, making it easier for others(or yourself at a later date) to find.
For example:
/index.php
/templates/index_view.php
While often the file extension of the template file can be changed to allowing easier identifying (say .tpl) for small quick deployments leaving it as .php means that no server configs need to change to allow it to run correctly and stop it from been inappropriately viewed.
Now include in your index file all the applications/sites logic.
Include in your index_view file all the HTML or visual based markup. Where a dynamic variable is required use php to echo it out. For example or
While you can include the template file in scope in your php file, it’s neater and more flexible to create a separate function to complete this task (or if appropriate and as it becomes more complex a separate Class may be desirable). In addition this step is required if you wish to use the templates for email.
/**
*
* Apply template parse view file, store and return output.
*
* @param $template_name String The name of the template file without it's folder or file extension.
* @param $params Array Associative array of parameters required by the template file
* @return String the final output.
*/
function apply_template($template_name,$params) {
extract($params);
ob_start();
include('./templates/'.$template_name.php');
$content = ob_get_contents();
ob_end_clean();
return $content;
}
The above is a basic function that will set local variables with the name of the key in the associative array of params, include the template file in this scope, capture output and return as string.
Below shows how this may be used with an example of the index.php and template index_view.php files.
In index.php
/** PHP file to get a string and output it **/
$msg_str = 'Hello world';
$params = array('message' => $msg_str);
$template_name = 'index_view';
$output = apply_template($template_name,$params);
echo $output;
In index_view.php
<?php
/** PHP File to display a name **/
?>
<html>
<head>
<title>Message Output</title>
</head>
<body>
<h1>Example Template</h1>
<p><?= $message; ?></p>
</body>
</html>
That’s it for the basics, extra loops and where necessary conditional statements may be added to the template file to make it more powerful but avoid crowding it with any business logic, try keep it as neat and pure HTML as possible.
In addition where the above can be extra useful is rather than echoing it out as is, the output from can be used as the HTMLl email content.
For example:
$output = apply_template($template_name,$params); mail($to, $subject, $output, $headers);



Those of us already dabbling in 

In these posts I will be publishing code snippets, tutorials and plugins that I have used (or created) while making sites for Conduct HQ.