Comparison of PHPTemplate and Twig Paradigms
About Twig
Twig is a compiled template language based on PHP. When your web page is rendered, the Twig engine takes the template and converts it into a "compiled" PHP template, which is stored in a secure directory at sites/default/files/php/twig. Compilation happens once, template files are cached for reuse, and recompiled when the Twig cache is cleared.
The Drupal Twig initiative shares the same motivation as Symfony’s: to introduce a modern, powerful, object-oriented engine that allows developers to properly focus on Drupal.
1. Docblock
PHPTemplate:
Twig:
{# /** * @file * File description */ #}
2. File and Function Names
PHPTemplate file: node--article.tpl.php
Twig file: node--article.html.twig
PHPTemplate function: THEME_node_links()
Twig file: node-links.html.twig
3. Variables
Print a variable:
PHPTemplate: <div class="content"><?php print $content; ?></div>
Twig: <div class="content">{{ content }}</div>
Print a hash key element [1]
PHPTemplate: <?php print $item['#item']['alt']; ?>
Twig: {{ item['#item'].alt }}
Assign a variable:
PHPTemplate: <?php $custom_var = $content->comments; ?>
Twig: {% set custom_var = content.comments %}
Assign an array:
PHPTemplate: <?php $args = array('@author' => $author, '@date' => $created); ?>
Twig: {% set args = {'@author': author, '@date': created} %}
4. Conditions
PHPTemplate: <?php if ($content->comments): endif; ?>
Twig: {% if content.comments %} {% endif %}
PHPTemplate: <?php if (!empty($content->comments)): endif; ?>
Twig: {% if content.comments is not empty %} {% endif %}
PHPTemplate: <?php if (isset($content->comments)): endif; ?>
Twig: {% if content.comments is defined %} {% endif %}
PHPTemplate: <?php if ($count > 0): endif; ?>
Twig: {% if count > 0 %} {% endif %}
5. Control Structures
PHPTemplate: <?php foreach ($users as $user) {} ?>
Twig: {% for user in users %} {% endfor %}
6. Filters
Escaped HTML special characters:
PHPTemplate: <?php print check_plain($title); ?>
Twig[2]: {{ title }}
Raw output:
PHPTemplate: <?php print $title; ?>
Twig: {{ title|raw }}
Translate:
PHPTemplate: <?php print t('Home'); ?>
Twig: {{ 'Home' | t }}
Translate with substitutions:
PHPTemplate: <?php print t('Welcome, @username', array('@username' => $user->name)); ?>
Twig: {{ 'Welcome, @username'|t({ '@username': user.name }) }}
Drupal 8 Twig (with trans tag extension):
{% set username = user.name %} {% trans %} Welcome, {{ username }} {% endtrans %}
Join a list:
PHPTemplate: <?php echo implode(',', $usernames); ?>
Twig: {{ usernames|join(', ') }}
Join a list with markup:
Twig: {{ usernames|safe_join(',') }}
PHPTemplate example requires $usernames to be an array of strings. Same for the original Twig example. However, Drupal 8 Twig expects a render array. This is a fundamental difference between original Twig and Drupal 8 Twig, which can render both plain text and render arrays.
Another aspect: all examples are expected to output the same result, but they don’t (by default). Example:
{% set numbers = [{'#markup': 'One'}, {'#markup':'Two'}, {'#markup':'Three'}] %} {{ numbers }}
This assumes comma separation, but the output is: OneTwoThree
7. Whitespace Control
Twig includes whitespace control to remove spacing used for template file structure.
{{- block.content -}}
Equivalent to:
{{ block.content }}
Notes
- The hash key element example may change in the future
- Second example shows Twig handles data sanitation. Previously, the template or preprocess function handled this. For those creating PHPTemplate themes for Drupal 8, you must now sanitize your own data. ↩︎︎
For More Information:
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.