Join

Main navigation

An Introduction to Twig Template Engine

by
Tamer Zoubi

CEO & Co-founder

14Nov2014

Twig is one such fairly new engine, built for PHP. An author of Twig claims that although PHP is considered a kind of template processor, over time the development of PHP itself, along with the options it gives, ventured away from what is expected from a modern template system. In comparison to using PHP itself as a template engine, Twig claims to offer improved conciseness, template oriented syntax, features, extensibility, documentation, security, error messages and speed.

Template Engines

The definition from this article, based on consensus from primary literature:

A template processor (also known as a template engine or template parser) is a piece of software or a software component that is designed to combine one or more templates with a data model to produce one or more result documents.

There is also a separate but relevant article titled Web template systems which gives some additional illustrations and points and covers a general case of template engine use in the context of the Web.

About Twig

Twig is one such fairly new engine, built for PHP. An author of Twig claims that although PHP is considered a kind of template processor, over time the development of PHP itself, along with the options it gives, ventured away from what is expected from a modern template system. In comparison to using PHP itself as a template engine, Twig claims to offer improved conciseness, template oriented syntax, features, extensibility, documentation, security, error messages and speed.

Personally, my favorite is conciseness which is basically more streamlined and simplified syntax we can use by means of Twig when we output the content of data. This gives us the benefit of greater code readability and, as a consequence, maintainability.

Introductory How-to and Tutorial

The version of PHP I have on a particular machine I'm using to review Twig is "5.5.3-1ubuntu2.6" which is high enough (minimal version of PHP needed is 5.2.4) so that I can immediately proceed to install Twig:

Searching for others with the same error in this context doesn't yield any significant results on Google which leads me to conclude that this usage scenario doesn't work out of the box as such. The problem is a missing array keyword, which means we have to change the Twig_Loader_Array input parameter to "array( 'index' => 'Hello {{ name }}!' )" after which everything works OK and index.php outputs the following:

The cache is false by default, so there'll be no difference if we omit the whole configuration array as a second parameter. In high throughput production environment, you will probably need to enable, set and configure caching properly. Here is a useful blog post detailing issues that might be of concern at that point.

Let's update index.php again with a simulated article content, let's say we are building a minimalistic CMS or something similar and let's presume that we have loaded some array with all the related article content we need on some particular page:

This is all good, except for indentation inconsistencies between template and output. To compensate for them by manually deleting and inserting whitespaces in template itself would defeat the purpose of using a template engine to a certain extent, in terms of code readability. This particular issue probably has to do with whitespace artefacts coming out from Twig itself. The issue is present whether spaces or tabs are used. Perhaps playing with whitespace control might alleviate it, at the cost of more code in the template itself.

Conclusion

Indentation issues aside, heavy use of template engines in various SaaS systems like Shopify and Desk.com is contributing to their rising use and they are probably not going away any time soon. Templating as such has also found its use in CMS systems like Drupal 8. What these two scenarios have in common is the fact that you can give access to a template file to a person who only understands a little bit of HTML and still have him be able to modify system's HTML/CSS output in a practically safe manner, compared to letting hir mess with server side scripts or some obscure and delicate parts of CMS, perhaps with a theme layer of Drupal 7. This, along with manyotherfeatures which templating provides, is what makes the usage of template engines such as Twig quite useful.