Tutorials
Simple PHP Template System
I found an article from a few years back written by a guy named Brian Lozier that totally changed my mind on using a templating system in PHP. I’ve gone through quite a few template systems and had settled on Smarty. In fact, I’d been using Smarty for all my projects – it’s a great program.
Then I read this article on Template Engines and realized that Brian was absolutely right. I could understand (sort of) why you might need a complex meta-language like Smarty if you’re trying to literally block your template designers from using PHP. Even then, is it really worth the extra layer? When you stop and think about it on a practical level, it’s adding an extra step that could easily be removed. Cut out the middle man.
I was so enamoured by this idea, that I took the core of his template class and began converting my base code from Smarty to this new ‘Simple PHP Template System’. This is basically the same code Brian was using, with a few tweaks and modifications for my own twisted purposes. I like the caching system, although I cut it out of this first version just to make things as incredibly simple as possible.
Here’s how to get it working. First grab the template class and save it in a directory as lib.templater.php:
<?php
/**
* Simple PHP Template System
* This was built on and inspired by an article from Brian Lozier:
* http://www.massassi.com/php/articles/template_engines/
*
* @author Kirk Brown <kirk@kirkbrown.com>
*/
class templater {
/**
* Template directory
* @var tplDir
*/
var $tplDir;
/**
* Template variables
* @var vars
*/
var $vars;
/**
* Constructor function for the templater
*
* @param string $tplDir template directory
*/
function templater($tplDir='') {
$this->tplDir = $tplDir;
}
/**
* Sets a variable for later template parsing
*
* @param string $name name of the variable
* @param string $value value to replace it with
*/
function set($name, $value='') {
if (is_array($name)) {
foreach ($name as $key => $value) {
$this->vars[$key] = $value;
}
} else {
$this->vars[$name] = $value;
}
}
/**
* Parses a php file with all current $vars
*
* @param string $file file to parse
*/
function parse($file) {
if ($this->vars) {
extract($this->vars);
}
ob_start();
if (is_file($this->tplDir . $file)) {
include($this->tplDir . $file);
} else {
include(PATH_TPLS . $file);
}
$parsed = ob_get_contents();
ob_end_clean();
return $parsed;
}
}
?>
Next pick up the sample template, be amazed how simple it is, and save it in the same directory as sampleTemplate.php:
<h1><?= $page ?></h1>
<table class="dTbl">
<tr>
<? foreach ($headers as $header) { ?>
<th><?= $header ?></th>
<? } ?>
</tr>
<? foreach ($rows as $row) { ?>
<tr>
<? foreach ($row as $cell) { ?>
<td><?= $cell ?></td>
<? } ?>
</tr>
<? } ?>
</table>
Finally, get the page that glues it all together so you can see it in action and save that in the same directory as viewTemplate.php:
<?php
include('lib.templater.php');
$tpl = new templater();
$tpl->set('page', 'This is my Page');
$tpl->set('headers', array('One', 'Two', 'Three', 'Four'));
$tpl->set('rows', array(
array('abc', 'def', 'ghi', 'jkl'),
array('mno', 'pqr', 'stu', 'vwx')
));
$html = $tpl->parse('sampleTemplate.php');
print $html;
?>
That’s it. If they’re all in a directory together, you should be able to see it in action by running viewTemplate.php. Let me know what you think, I’d love to improve on this without ever making it too bloated.
If you don’t want to cut and paste, you can download the files in a zip
Copyright © 2009 Kirk Brown
XHTML - CSS