Template Engines

July 25th, 2006 Comments Off

My recent article about PHP frameworks details the many different avenues one can travel when in need of a PHP framework. Just like frameworks, template engines in PHP also give you a lot of different options. Options can be good, however their are advantages to the different forms of template engines.

Compiling

Some template engines like the popular Smarty template engine, compile templates. This means that template files have there own syntax, and then Smarty will compile this syntax into PHP code which will be used to display your template. For the longest time I used the Smarty template engine, until I learned that this might not be the best solution. PHP already acts as a template engine in it self, and even though Smarty carries lots of advantages, it arguably doing more work than it needs by compiling templates into PHP code. In addition, there is a bit of a learning curve in order to write templates in the Smarty scripting language. Other template engines, like Savant, build a nice template engine around PHP. This means that there is no extra scripting language to learn, and no extra resources used to compile your templates into PHP code. I have recently switched most of my sites over to the Savant engine and seen a slight performance increase because Savant is not compiling templates on the fly like Smarty.

Caching

Some template engines, again like Smarty, have caching options that will cache you templates so that they do not need to recompile your template every time a page is requested. This is avoided in template engines that do not compile templates. Caching is a good feature, but sometimes hinders the way a site operates. Lets say if you have a side bar that displays information dynamically, with caching changes will not be live instead they will reflect the cache settings on how often the data is updated. There are several ways around this, but it is something to consider when choosing a template engine and deciding what works for your situation and needs.

Plug ins

Most all template engines have a plug in architecture, and depending on how active of a community supports the project; there will be a directory of pre-made templates. One thing to look for in a template engine is not only what plug ins you will need, but how are the plug ins loaded. You do not want a template engine that will load all plug ins installed. Rather a better approach is allowing you to have control on when a plug in is loaded. Think about it: if a plug in is only used in one section of your site, you do not want that plug in loaded into memory for every other page on your application when it is not needed. Unused plug ins should not take execution time to load and memory resources when they are not going to be used.

Install Globally

When you decide on a template framework to go with, install it globally on your server and include it in you php.ini file. This will allow you to use it for numerous websites on your server and only have to upgrade the template framework in one place when updates are available. This is a good tip to follow for any other classes that you use across applications, it will give you one place that will include all the necessities for your applications.

Comments are closed.

What's this?

You are currently reading Template Engines at Code and Coffee.