Different PHP template engines


Originally posted in my old blog at My Opera

While working on the Smarty View Helper solution I mentioned in the previous post, I went through a lot of Smarty's code and I also did some searching on their forums.

Smarty's code isn't very modification friendly and they had discussed associative array support for Smarty in their forums in 2003 – That's 4 years ago, and they still haven't got it in Smarty!

So, this got me thinking what alternative template engines are available. If Smarty hasn't got any real new features in a long time, others should have them. Right?

Here's a list of template engines I'll review and compare to Smarty, some more in depth than others: Blitz, patTemplate, vlibTemplate, SmartTemplate, Expose, Savant and PHPTAL.

What I'm looking for

This is what I'm looking for in a template engine. I know that you aren't necessary needing all this or don't agree with me, but this should help you understand my opinions on the other engines.

  • Clean syntax. I want the template syntax to be short and clean like Smarty's. This doesn't necessarily mean the engine has to use a custom Smarty-like syntax, but often PHP-based solutions have very long syntax that I don't like.
  • Extensibility. Most engines can't provide all functionality I want, so it has to be extensible. Preferably by providing you the possibility of writing plugins or such.
  • Speed. Using it shouldn't have a very significant impact on the site's speed.

Different types

So, what kinds of other template engines are out there? You could categorize most of them into three groups:

  • Compiling engines. Template engines like Smarty, which use a custom template language and compiles them into PHP files for faster execution. Advantages of compiling engines are the custom syntax and security as what you can put in the templates is limited. Disadvantages are the custom syntax, because you may have to learn it, bad portability and often limited functionality.
  • PHP-based engines. They use PHP and some custom functionality. Personally I find PHP-based template syntax very long and not very nice looking. Advantages are familiar PHP syntax so you won't need to learn many new things and good functionality as you can use anything PHP provides you with. Disadvantages are long syntax which can also be slightly confusing to non-programmer types. Also, not very portable.
  • XML-based engines. These use XML based templates. Some may use XSLT for transforming them into HTML. Advantages and disadvantages similar to compiling engines. They also often double as HTML validators, as they parse your templates with an XML parser, so it helps you make sure your code is valid. With pure XML+XSLT, you get good portability as most programming languages have XML+XSLT support.


Most of the syntax for PHP and compiling engines is quite similar. PHP-based engines usually use var; ?> or something similar. Compiling template engines tend to use { and } to delimit template tags, but you can usually change the delimiter if you feel like it.

Often the engines also use a similar style of initialization and variable assignment:

$engine = new EngineName();

The template engines also always provide you with common flow-control structures: if-else, for, while and such. Many also let you perform different operations on template variables, such as escaping HTML entities or limiting length.

Let's review!

So, let's first look at the less interesting engines and leave the more interesting ones for later.

  • SmartTemplate: Small, fast, syntax very much like Smarty's. However this is where the good things end. It's buggy, doesn't have much functionality in the template syntax and doesn't let you use plugins.

    It might get better, though. It was recently picked up by new developers and was renamed to QuickSkin. For example, they've added support for extensions to it.

  • Savant: PHP based. Syntax long, _($this->varName); ?> for echoing a variable. Doesn't compile as it uses PHP, but it does support user-defined custom compilers if needed. Supports plugins. Overall a good engine if you like the PHP syntax.

    Easy to get started as it requires less setting up than Smarty (Smarty needs you to set the compile dir and things like that)

  • vlibTemplate: Another Smarty-like compiling engine. Has a very similar feature-set as SmartTemplate, but comes with caching and a debugger.

Okay so that's it. Of those three, Savant is possibly a very good option if you're looking for a flexible template engine. I just don't like its syntax.


Blitz homepage

Now this is a quite interesting take at a template engine: It's built as a PHP extension, so it should be very fast. The syntax is pretty similar to SmartTemplate's: {{ varname }} and uses BEGIN and END for loops.

The featureset is quite basic: it has some flow control structures, output escaping and template including. No support for template plugins or such, so it's not really extensible.

Can be difficult or impossible to install as it's a PHP extension. I wasn't able to get it installed on my host at all.


PHPTAL homepage

PHPTAL is based on ZPT, Zope Page Templates, and works only with PHP5. It uses XHTML attributes for template logic, so it's an XML based engine and requires you to write XML-valid HTML/XHTML, which is mentioned as a drawback in the docs for some odd reason!

The syntax is very interesting: In PHPTAL, the following code…

<?php foreach ($items as $item): ?>
<div><?php echo $item; ?></div>
<?php endforeach; >

looks like…

<div tal:repeat="item items" tal:content="item">
this is just example content and is not required, kind of
like a comment. Neat!

That may look a bit confusing at first, but I kind of like it. The fact that you can insert example content inside the elements is also quite nice, as it acts like a comment and you can view the template files even outside the PHP backend.

It supports macros, which are kind of like including subtemplates. Also supported is internalization, with the built-in translation classes.

PHPTAL lets you run PHP in the templates as well, so if you can't do something with its own language, just have it run some PHP. You can also create custom plugins, called expressions.

This is a full-featured and powerful template engine.


patTemplate homepage

This is an another XML-based engine, but differs much from PHPTAL. This is more similar to using XML and XSLT. You define templates in an XML file and give them names, quite similarily to using xsl:template.

patTemplate doesn't have separate flow-control functions like looping or if-else's, it's all handled by the main patTemplate:tmpl element.

To make a loop, you would define a template for whatever content you would like to be looped, such as table rows. Then, you assign an array of data to patTemplate in PHP. This will make the template act like a loop. If you were to assign just a single value, it wouldn't loop. You can also define required values for templates in the XML code, which will make it work like an if-clause.

patTemplate example, taken from the manual.

<patTemplate:tmpl name="foo">
  This is a simple variable: <patTemplate:var name="simple"/>, it is identical to using {SIMPLE}.
  This is a variable with a default value: <patTemplate:var name="foo" default="bar"/>.
  This is a variable which is copied from another variable: <patTemplate:var name="bar" copyFrom="foo" modifier="ucfirst/>.
  This is a variable with a default value created by a PHP function: <patTemplate:var name="timestamp" default="time()"/>.

patTemplate also lets you define your own custom template tags.

This is also a very good template engine with lots of features.


Expose homepage

Expose is like a PHP-based and a compiling engine in one. Its syntax is very PHP-like, but it compiles the templates. You can use PHP almost like you would with PHP-based engines, but you aren't limited by what PHP tells you.

For example, you can use short tags for echoing even if short_open_tag is off, making for much shorter syntax. You can use PHP functions with Expose, but you may have to register them with the Expose class first.

Expose also has built-in localization support, let's you define template plugins and has a caching-mechanism.

If you are looking for a powerful engine with good features and short syntax, Expose could be a good choice to try out.


There are a lot of template engines around, but many of them aren't as powerful as Smarty. And despite its ease of use, none of the engines with very Smarty-like syntax seem to have a good feature set.

Fully PHP based Savant looks like a good choice, but the syntax is long and ugly. Expose on the other hand has a lot of features and even though the syntax is like PHP's, it can be kept short because of its own compiler. PHPTAL and patTemplate are in their own league because of their XML-based parsing: both have a very distinct syntax, but still provide much features.

In the end it is based on the programmer's own opinion which of these he/she likes the best. You can say which one has the richest features, but things like syntax and ease of use split opinions a lot.

Which of the engines would I pick? Most likely Expose, pat or PHPTAL. All three seem like solid choices and I will most definitely be trying them out more in the future. But could any of them perform what I did to Smarty in the previous post?

Since both PHPTAL and patTemplate use a very different syntax, I can't really apply it to them, but would Expose bend to it? Most likely yes. As Expose is a compiling engine, you would just need to go in the code and change it so that every symbol that the compiler doesn't understand is tried as a view helper, in the same fashion as the Smarty-mod.

Would I give up Smarty? Perhaps. I will need to give some serious testing for those three and we'll see. Being able to get good integration with the Zend Framework is a must, though!