Cognimatic

Search form

Twig in Drupal Core

2 Jun 2013

Drupal's templating system is moving to Twig with Drupal 8, due for release later in 2013. It is more a secure and better supported approach than PHPTemplate.
Apart from minor changes to the theme file structure, the templates themselves no longer allow PHP to be embedded, with a list of predefined tags (for control structures) and extendable functions and variables to render output.

Last week it was announced that Twig will be incorporated into the next release of Drupal, version 8, due for release later in 2013. There's still a lot of work to do, but I thought I'd scratch around to see what the significance is of this big change to Drupal theming, and why it should be of interest.

Drupal has used a templating system in order to separate business logic from the presentation layer. In practice, it means building the content of the website, and it's presentation separately; the site can also have a theme updated periodically, without directly affecting the functionality or content of an existing site. Up until Drupal 7 the PHPTemplate system had been used to generate Drupal themes.

What is Twig?

To quote the Twig website:

Twig is a modern template engine for PHP:

Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.

Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design.

Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.

It's also worth pointing out that Twig is written by Fabien Potencier, creator of the Symfony framework, which has – in part – been incorporated into Drupal 8.

The main advantage will be improving security to Drupal; there are supposedly performance improvements (the templates are effectively compiled into a PHP file), but this outcome is largely dependent on work in the next few months whilst the issue queue is worked on. Twig already used by lots of other frameworks (Zend, CakePHP, CodeIgniter) and is widely supported in many IDEs: Eclipse, Netbeans, Textmate, Coda, Komodo etc, so should allow Drupal templates to be created by a wide community of designers.

Functions can be called to generate content. Functions are called by their name followed by parentheses () and may have arguments. For instance, the range function returns a list containing an arithmetic progression of integers:

// Divides two numbers and returns the truncated integer result (ie, rounded down)

* Multiplication

** Raises to the power

Logic

and

or

not

(expr): Groups an expression

Variables - Filters

Variables can be modified by filters. Filters are separated from the variable by a pipe symbol | and may have optional arguments (in parentheses). Multiple filters can be chained: the output of one filter is applied to the next. The following example outputs text and applies the Drupal translate function:

Inheritance

One aspect that hasn't really changed is that there is inheritance, although in Twig it's a little truer to an object-oriented model, and uses the extends tag. You can also define what aspects of a parent template can be overridden by a child template, making the 'parent' theme more secure. include() can also be used to reuse components:

Changes to File Structure

mytheme.info becomes a Yaml file: mytheme.info.yml - it has a very similar manifest to PHPTemplate files.

There's still a bit of tweaking to be done to get Twig fully functional in Drupal 8, so keep a close eye on the Planet Drupal. There is also some useful material to follow up in the References below. May even be an Alpha release of Drupal 8 in the very near future...