There’s a ton of documentation available if you want to do template handling in PHP. This article is only about documenting the simple approach I use myself for a PHP template engine. I’m not going to enter the arena by stating PHP is a template language itself etc …, that’s just plain boring.
So what’s the intention here? The objective is to have a plain HTML file and replace content at certain places where you want PHP driven output to show. But by and itself the HTML file is just that, plain HTML with inclusion of CSS and JS where necessary.
My standard HTML file is shown below:
<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-language" content="{language}" />
<meta name="author" content="M.E. Post" />
<meta name="copyright" content="Copyright (c) M.E. Post 2008" />
<link rel="stylesheet" href="{includepath}/css/include.css" type="text/css" media="screen" />
<script type="text/javascript">
var path = '{includepath}';
</script>
<script type="text/javascript" src="{includepath}/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="{includepath}/js/include.js"></script>
</head>
<body>
<div id="rap">
<div id="headwrap">
<div id="header">
<a href="{path}/">{title}</a>
</div>
<div id="desc">
<a href="{path}/">{subtitle}</a>
</div>
</div>
<div id="content">
<div class="storycontent">
{replace_content}
</div>
</div>
</div>
</body>
</html>
As you can see it’s a very minimal file and there are some elements in there like {includepath} and {replace_content} which are not regular html. These are the placeholders where content will be replaced.
Replacing the content is executed by the function below. It gets the content transferred through the variable $content, if the $content variable is empty it returns FALSE and aborts the function. After that it checks whether the template has already been loaded through checking the static $template, if it’s empty the template file is loaded, otherwise it will reuse the previously loaded template. All the template placeholders are replaced through a loop using mb_ereg_replace to make the text unicode compliant. The replaced template is returned as output of the function. Items like PATH et al are constants that are defined previously, you can take them out or add them to the function call if you want.
/**
* Merge the page template with the content
*
* @param string $content
* @return string
*/
function mergeContentWithTemplate($content='') {
if (empty($content)) {
return FALSE;
}
/* Static keyword is used to ensure the file is loaded only once */
static $template = NULL;
/* If no instance of $template has occured load the template file */
if (is_null($template)) {
$template_file = dirname(__FILE__) . '/../html/template.html';
$template_file_content = file_get_contents($template_file);
}
mb_regex_encoding('utf-8');
$pattern = array('{path}', '{includepath}', '{language}', '{title}', '{subtitle}', '{replace_content}');
$replacement = array(PATH, INCLUDE_PATH, LANGUAGE, TITLE, SUBTITLE, $content);
$pattern_size = sizeof($pattern);
for ($i = 0; $i < $pattern_size; $i++) {
$template_file_content = mb_ereg_replace($pattern[$i], $replacement[$i], $template_file_content);
}
return $template_file_content;
}
So that’s my simple little template thingy, hope it is of some use to you.


