An introduction to Drupal 8 Twig, at DCD 2016

An introduction to Drupal 8 Twig, at DCD 2016

author By Pranav Gupta Nov 28, 2016
DCD 2016
Submit guest post

Every year this time around, anticipation builds at Srijan for the upcoming Drupal Camp. Being the newest member of the team, I got a chance to be a part of the same. Drupal Camp Delhi 2016 was my first time at a Drupal event, and it was super exciting. So here I am, sharing my experience of participating in learning sessions, talks, and code sprints.

A glimpse from the day

The keynote by Drupal veteran Shayamala Rajaram was well attended by the audience. She shared her insights on where Drupal stands today and a road map for the future. She also highlighted the tremendous opportunities for Drupal, what it is like to interact with a global community, and the experience of Drupal Cons all over the globe.

The keynote concluded with a short story about the Vixen & the Lioness which emphasized the importance of quality over quantity.

One of the sessions I attended was by Swastik Pareek, which was a basic introduction to Twig. 

Drupal 8 brings with it an entirely new theming layer and language: Twig. We got familiar with what it is, where it came from and how it differs from the PHP arrays of Drupal's current environment.

The session also took a look at the Drupal 8 Twig theme and examined the Twig syntax and functionality like looping and conditionals, that help us banish theme pre-processing from our workflow.

The new life cycle of a page gives us far more flexibility with how we write our themes, structure our templates, and control our markup.

Learnings from the session

What is Drupal 8 Twig ?

Twig is a modern template framework and is a direct replacement for PHPTemplate. It is in syntax for us to be using in Drupal 8, and to create and develop our Drupal themes.

Why another template engine ?

“PHP” template is old, and “broken”. Twig is more flexible, fast, and secure template engine for PHP.

  • Full Featured
  • Fast
  • Concise
  • Easy to Learn
  • Extensible
  • Unit Tested
  • Documented
  • Clear Error Messages
  • Highly secure

PHP Template -vs- Twig

A few examples to show some of the similarities and differences between PHP and Twig:

1. Docblock

PHP  Template                                   
<?php /**
* @file
* File description



2. File and function names

PHP Template
Twig file

PHP Template function

Twig file

Here we have file names and function names that are fairly similar but one thing that we have to remember is that in PHP, if we want to have node article, we have to write functions to get that to work at the page level. But we don’t have to do that with Twig, because it is built to have a whole series of different names that it will look for all the time. So if you need to override things down to specific node, you can do that without having to write any more functions to get it to pick up that file.

Note: All theme functions are now markup files

3 Basic Operators in Drupal 8Twig


Twig Debugging

Default: Twig compiles templates into PHP code and stores the compiled code in memory.
Development: Change the environment variable in settings.php to allow for different configurations per environment.

Twig debug: Surrounds Twig templates with HTML comments that contain theming information like template file name suggestions

Note: Debugging markup will cause automated tests to fall

The dump() function can be used to output info about template variables
Templates are automatically recompiled when source code changes(see twig_auto_reload below)

Not recommended in production environments(Default:FALSE)


Macros are comparable to functions in regular programming languages. They are useful to put often used HTML idioms into reusable elements, to not repeat yourself.

Macros differ from native PHP functions in a few ways:

  • Default argument values are defined by using the default filter in the macro body
  • Arguments of a macro are always optional
  • If extra positional arguments are passed to a macro, they end up in the special varargs variable as a list of values.

But as with PHP functions, macros don't have access to the current template variables.
Here is a small example of a macro that renders a form element:

When you want to use a macro in another macro from the same file, you need to import it locally:

So those were the Twig basics. But if  are excited to learn more about Drupal 8's fonend capabilities, you have to check out MortenDK's webinar on "Goodbye to 10 Years of Theming Headaches".

Final Thoughts

Coming to the climax of DCD 2016, it was clear that Drupal Camp was amazing and the audience was very energetic. Many digital onlookers were just as excited about the discussions and other sessions.

Some of my favorite moments were:

  • Getting to meet new people and find out what they do with Drupal and where they have come from
  • Learning and implementing the knowledge from sessions
  • A wonderful opportunity was open to all, to take up Acquia certification at low cost.
  • Lots of Drupal goodies
  • A representation from my organization
  • Post Camp sponsored party

So now I am already looking forward to the DCD next year, and hoping it would be as awesome as DCD 2016.


Write to us

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms Of Service apply. By submitting this form, you agree to our Privacy Policy.

See how our uniquely collaborative work style, can help you redesign your business.

Contact us