Using Twig in Drupal 8

Using Twig in Drupal 8

After the official release, more and more Drupal developers are interested in learning about the new features of Drupal 8. So here is a chance to learn more about the Twig template engine in the newest blog post from our developer, the author of the popular posts on confuguration in Drupal 8 and tips for Drupal 8 developers. Here we go!

As you probably know, Drupal 8 uses Twig instead of PHPTemplate. This means that now, instead of the usual *.tpl.php files, we use the *.html.twig files.

Twig vs PHPTemplate

So let's take a look at the main features of Twig and its differences from PHPTemplate.

Outputting a variable:

 <?php print $variable; ?>
 {{ variable }}

The If Operator:

 if ($variable_1 == “1”): ?>  <div>
 <?php print $variable_2; ?>
<?php endif; ?>
 {% if variable_1 %}
{{ variable_2 }}
{% endif %}
 <?php if (!empty($variable)):
$variable2 = ‘value’;
 endif; ?>
 {% if variable is not empty %}
set variable2 = ‘value ’
{% endif %}
 <?php if (isset($variable)):
$variable2 = ‘value’;
 endif; ?>
 {% if variable is defined %}
set variable2 = ‘value ’
{% endif %}
 <?php if ($variable < 0):
$variable2 = ‘value’;
 endif; ?>
 {% if variable < 0 %}
set variable2 = ‘value’
{% endif %}

Assigning a value to a variable:

<?php $variable = ’some_value’; ?>
 {% set variable = ‘some_value ’%}

Creating an array:

 <?php $my_array = array(1,2,3,4,); ?>
 {% set my_array = [1,2,3,4,]%}
 <?php $my_array = array('!element1'=>$var1, '!element2'=>$var2); ?>
 {% set my_array = {'!element1': var1, '!element2': val2} %}


 <?php foreach ($users as $user) {
} ?>
 {% for user in users %} {%
 endfor %}

Filters in Twig templates

In Twig templates, you can use various filters that change the variable outputting format directly in the template.

The available filters:

abs - returns the absolute value.


 {# number = -5 #}
{{ number|abs }}
{# outputs 5 #}



{% set items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] %} <table> {% for row in items|batch(3, 'No item') %} <tr> {% for column in row %} <td>{{ column }}</td> {% endfor %} </tr> {% endfor %} </table>


        <td>No item</td>
        <td>No item</td>

capitalize — transforms the first character to uppercase and all the others to the lowercase


{{ 'twig in Drupal 8'|capitalize }}


 Twig in drupal 8

date — formats the date according to the specified format.


 {{ '1447337804'|date("m/d/Y") }}



json_encode — returns JSON data presentation.

The json_encode PHP function is used.

first — returns the first element of the array or the line.


 {{ [1, 2, 3, 4]|first }}
{# outputs 1 #}

{{ { a: 1, b: 2, c: 3, d: 4 }|first }}
{# outputs 1 #}

{{ '1234'|first }}
{# outputs 1 #}

length — returns the first element of the array or the line.

lower — transforms the value to lowercase.

number_format — is filter to format numbers.


 {{ 9800.333|number_format(2, '.', ',') }}



In order to process the variable through the t() function, you must use the t filter.

It looks like this.

 <a href="{{ url('<front>') }}" title="{{ 'Home'|t }}" rel="home" class="site-logo"></a>

Debugging the Twig templates

To enable the option of debugging the Twig templates, go to the services.yml file and assign the true value to the debug parameter in the twig.config settings block. For easier development, you can also assign the false value to the cache parameter. This means, you will not have to clean the cache after making changes in the templates.

    debug: true
    cache: false

Setting your own template for an element

To set up your own template for an element, do the following:

 function my_theme_theme($existing, $type, $theme, $path) {
  return array(
    'block__system_menu_block' => array(
      'template' => 'custom-menu-template',


block__system_menu_block is the element for which you want to use the template;

custom-menu-template is the name of the template (the template filename will look like this: custom-menu-template.html.twig).

Hope this information will help you understand Drupal 8 better and make your development process easier and more enjoyable!:)

9 votes, Rating: 4.9

Read also


In one of the recent blog posts by our developer, we offered you Drupal 8 development tips. Today, we'll...


Hello, everyone! The release of Drupal 8 is almost here, but its beta version is available for use now. So let...


It’s popular, free, flexible, powerful. It’s just Drupal! All Drupal teams know that this CMS has had 7 official...


Beta version of Drupal 8 is already released. And right now InternetDevels team is developing our first project using it. Dealing with Drupal 8 very close we’ve prepared a review of its most...

Subscribe to our blog updates