Understand why Twig is replacing PHP Template in Drupal

Understand why Twig is replacing PHP Template in Drupal

On14th Aug 2021, 2025-01-20T16:50:58+05:30 ByKarthik Kumar D K | read
Listen Pause Resume Stop

A completely new template engine is introduced in Drupal. The templating system in all previous versions of Drupal is using theme_* functions and PHP-based *.tpl.php files. Now, this has been completely replaced in Drupal 8. Instead of using theme functions and theme template files, Drupal is now using the Twig templating system.

Twig is a PHP-based compiled templating language. When your web page renders, the Twig engine takes the template and converts it into a 'compiled' PHP template which is stored in a protected directory in sites/default/files/php/twig. The compilation is done once, template files are cached for reuse and are recompiled on clearing the Twig cache.

Why Twig?

If we already have PHP templates, a faster templating system…! Then why Twig?.

Let’s see how we define faster, is it execution time, or development time, or maintenance time, or memory utilized by it, or a combination of a few of these. To have a template engine means to have some performance improvements in terms of speed & memory at the same time have better development + maintenance experience.

And Yes, just using the PHP Templating system is faster compared with having any template engine. But at the same time, if any template engine is solving some problems and gives developers a better experience, then that would be a good choice for any framework like Drupal to have.

Below are a few points put out which we can take as improvements in a template engine compared to plain PHP templates.

1. Syntax in template engine

Drupal 8 will be accessing all the variables very consistently like {{ node.nid }} or {{ node_url }}, wherein Drupal 7 we have multiple ways to print a particular variable, including strings, objects & arrays.

2. Inconsistency in PHP templates

In Drupal 7 we can use template files and at the same time, we can use the functions to render. Sometimes it becomes harder to understand already written code or the Core code (without documentation) & also we see difficulties in overriding the code. But Drupal 8 only uses template files.

3. Redundancy while writing code

In Drupal 8 we can use the existing template, like using extends, which is a kind of proper inheritance and by this, we can eliminate the duplicate code. But in Drupal 7 we have multiple files with similar code.

4. Sanitize data

Using template engines like Twig, we can automatically sanitize the data which is to be rendered. So there would be no way to have any unsafe functions like in Drupal earlier version like in Drupal 7 we can print the unsanitized data to render & even we would be able to do database queries. Here Twig is responsible for sanitizing data.

5. Caching

And also handling of caches are improved in Drupal when compared to earlier versions (which is discussed in this article), which will keep any template engine at top of its performance.

Overall, Drupal as a framework now has moved to a more efficient and optimized way of rendering a site’s theme through the use of Twig.

Related Articles

Recent Articles

Recent Quick Read

Recent Great People

We Need Your Consent
By clicking “Accept Cookies”, you agree to the storing of cookies on your device to enhance your site navigation experience.
I Accept Cookies