Compelling arguments to integrate Twig template engine in Drupal 8

Aug 03, 2017
Compelling arguments to integrate Twig template engine in Drupal 8

A lot of time has passed since the release of the latest version of Drupal. However, the community moves on, and often delights experienced developers and users with the big improvements in D8. Drupal provides great opportunities and tools to make your website as accessible as possible or increase its speed to the maximum. Nice contributed modules for multilingual functionality and some content editing improvements are also in the list of the benefits of D8. In short, Drupal can easily meet all your specific wants and needs. Today, you have a chance to learn more about Twig, an easy-to-use modern powerful tool in Drupal 8.

Since 2005 PHPTemplate has been driving Drupal templates. Now Drupal 8 uses Twig as the template engine for the PHP. It is developed and maintained by Fabien Potencier, the founder of the Symfony2 framework. In order to accelerate development in Drupal and save time, Twig compiles templates to optimize PHP code. The usual *.tpl.php files are replaced by the *.html.twig template files. Twig allows web designers without any PHP skills, to make changes in the page markup. Obviously, this also makes theming in Drupal 8 easier to understand and perform. It is an excellent alternative to the PHPTemplate. Let’s take a look at some more benefits of this efficient framework.

Arguments in favour of Twig


No doubt you can find projects for which the embedded PHP language will be also suitable. But compared to it, Twig has a more concise syntax that makes templates more readable. It is easier to understand and easier to compile. Put simply, Twig allows you to write more efficient templates using a beautiful syntax that is pretty close to plain HTML.


You know that time is money. A site that loads quickly is popular among users and enables them to get the information they need as soon as possible. To be fast during site development stage is also crucial for the success of the whole project. One of the goals of Twig is to achieve the best speed possible as well. To achieve it, Twig compiles templates down to PHP code. The above mentioned optimized and easy-to-read syntax also helps web designers to get their job done fast.


Security matters anywhere. Making your website secure is as vital as providing security in the process of its development. In this respect Twig has some unique capabilities:

  • HTML escaping (automatic output escaping) — for safety reasons you can enable automatic output escaping for a part of code or the whole
  • Sandboxing — In the sandbox environment the developer determines a limited set of filters, tags and object methods. The user has access to it, and this allows Twig to be used as a template language for apps where users can change the template design themselves. Twig can evaluate and automatically isolate any untrusted template code there. You can enable sandboxing for some templates or globally.

By the way, the Twig library is fully tested and approved by the community. It is stable and ready to be used within a variety of projects (apps or websites). And we can’t help but mention the new syntax one more time. Logically, if you get clean concise templates with the new syntax, you get more security.


This useful feature can meet all your needs, whether simple or complex. Thanks to Twig’s flexibility and open architecture, you can add brand new functions, filters, tags, and even syntax and keywords. In general, the possibility to extend provides developers and web designers better tools to define their project structure in Drupal 8.


As Sensiolabs, the creators of Twig, confirm: “The most powerful part of Twig is template inheritance.” You won’t find this extremely powerful feature in some other templating languages. After implementing Twig, you will have no need to copy the parent theme template files into your custom templates. It enables you to create an universal layout and then inherit these layouts while only defining the blocks of code you want to replace. Extending a template {% extends ‘template.html.twig’ %}, you can manage any part of the original file and cut down the amount of template files and code you need to create your theme.

Of course, that’s not all the impressive features of the Twig template engine. Being a breath of fresh air to developers and web designers, it can accelerate the performance of your Drupal website. So, integrate Twig today with our Drupal experts!

 Get new blog posts by email