Posted September 28, 2010 by Spyros in PHP Programming

A Fast Template Engine For PHP


There comes a time in the life of a serious PHP programmer where they need to code (or be part) of a large scale project. Using the right tools is what separates the men from the boys. Virtually every well known large scale software written in PHP uses a template engine of some kind, with Smarty being one of the most well known ones.

However, convenience comes with a price. Because of using a kind of syntactical analysis, Smarty is a quite slow addition to your programs. On the other hand, not using a template engine at all, especially if presentation is complex, will result in creating ugly and unmaintainable program. Brian Lozier comes to our rescue. This great guy has created a very powerful template engine that is a hell lot faster than Smarty. Actually, is as fast as plain PHP is !

You can take a look at this great article, called Beyond the template engine. It’s really a nice read. In the fifth page of his article, you can see what the whole engine looks like codewise. As you see, it’s pretty small and easy to use. Just include the code in a php file and then include it in your project.

Now, go about creating a global singleton like object like :

$GLOBALS['templateObj'] = 0;

Now, to use it simply do :

$tpl = templateObject();
$commentsNumber = 3;
$tpl->set('commentsNumber', $commentsNumber);

echo $tpl->fetch('templates/comments.tpl.php');

Like using one of the well known templates engines right ? Only faster ! Keep this in mind and why not, use it in your next big PHP project.