The drupal 7 implementation of TWIG template system.

Documentation for this project is available at to, and a base theme is available at Soid sandbox

Twig for Drupal 7 could be a nice starting point of creating theme's that will be portable to Drupal 8, not 100% portable but with significant less work then converting all your tpl.php files to .twig files. Especially if you make the switch from theme_ methods to create field output to actually creating a .tpl.twig for your field (field__field_name.tpl.twig) and use the autorender functionality provided by this engine ({{content.fieldname}})


Please use the 7.x.3 branch instead of the 7.x.2 version. The engine and TFD libraries are the same but the installation process is way simpler!

Three easy steps, no copy/move needed

drush dl tfd7-7.x-3.0-beta1
cd themes/engines/tfd7/
composer install

Creating a twig theme

Creating a Twig theme is actually quite easy, you simply replace the engine = phptemplate with engine = twig and done.

name = Twiggy
description = A Twig theme
screenshot = screenshot.png
engine = twig
core = 7.x
php = 5.4

Please note, due to a issue in the Drupal 7 core, a Twig template can NOT extend a PHP Template. I did Try to patch it, but it got stuck because of the backport policy for core.


Twig for Drupal is mostly autoconfigured with sensible defaults, however you might want to fixate the location of where the template cache is writen. By default it goes into /files/twig_cache of your site. But you can override this behaviour by setting the file_twigcache_path in your settings.php

$conf['file_twigcache_path'] = 'path_to_cache';

Just make sure that your webserver has the rights to write files and create directories.

Turn of the automatic rendering of variables. You have to use {{render(var)}} instead.

$conf['twig_autorender'] = false;

Turn of the automatic html escaping for all the templates

$conf['twig_autoescape'] = false;

Turn of the auto reloading of templates, this could save some performance on production environments. But you must clean the template cache yourself if you change the templates in your project!

$conf['twig_auto_reload'] = false;

Extending Twig for Drupal 7

TFD Implements 3 hooks for developers to create their own filters, functions or even a specific test.

The default Twig for Drupal filters are implemented in sites/all/libraries/TFD/TFD/Extension.php take a peek in there to understand most things going on under the hood.


To register a new filter you should implement hook_twig_filter() and return an indexed array with unique keys and new Twig_SimpleFilter as value.


function my_module_twig_filter(){
    $filters = [];
    $filters['rot13'] = new Twig_SimpleFilter('rot13','my_module_rot13_filter');
    return $filters;

function my_module_rot13_filter($string){
    return rot13($string);

Optionally you can add the Twig environment as thirds parameter

<br>function my_module_twig_filter(){
    $filters = [];
    $filters['env_filter'] = new Twig_SimpleFunction('env_filter', 'my_module_env_filter', array('needs_environment' =&gt; TRUE));
    return $filters;

function my_module_env_filter($env,$string){
    // do something with the $environment
return $value;


To register a new filter you should implement hook_twig_function() and return an indexed array with unique keys and new Twig_SimpleFunction as value.

<br>function my_module_twig_function(){
    $functions = [];
    $functions['coolfunction'] = new Twig_SimpleFunction('coolfunction','my_module_coolfunction');
    return $filters;

function my_module_coolfunction($param_1,$param_2...){
    // do what you need to do, and return something.

You do not need to explicitly define the parameters of the callable, every para
meter you enter in the template will be passed to the callable.


To register a new test you should implement hook_twig_test and return an indexed array with unique keys and new Twig_SimpleTest as value.

By contract a Twig_SimpleTest should return TRUE or FALSE.

<br>function my_module_twig_test(){
    $test = [];
    $test['is_red'] = new Twig_SimpleTest('is_red','my_module_is_red_test');
    return $test;

function my_module_is_red_test($value){
    $value = str_to_lower($value;)
    return ($value ==== "ff0000" or $value === 'red');

The old 7.x.2 way of installing

By far the most complex thing if you are not a developer. Twig for Drupal relies on two contributed modules. X Autoload and Libraries API

Before you start building a Twig based theme, enable the modules above and copy some code the Libraries directory /sites/all/libraries/

Copy Twig

Download or clone the latest 1.x version from Github and copy the contents into libraries/Twig

Copy TFD

Download or clone the master or 7.x-2.0-dev version from Github and copy the contents into libaries/TFD

Move the twig.engine

After installing both libraries you should copy the twig.engine to sites\all\themes\engines\twig\

Final result.

After copying you should have something like this in your Drupal project.


Project Information