Experimental Project

This is a sandbox project, which contains experimental code for developer use only.

The drupal 7 implementation of TWIG template language.

Documentation for this project is moved to tfd7.rocks, development is done on https://github.com/TFD7/TFD7

The Drupal.org GIT is in sync with the Github version, the 7.x-2.0-dev is the same as the master branch, it only exist for Drupal.org convenience.

Installation.

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.

\sites\all\libraries\
\sites\all\libraries\TFD\
\sites\all\libraries\TFD\autoloader
\sites\all\libraries\TFD\TFD\
\sites\all\libraries\TFD\TFD\Loader
\sites\all\libraries\TFD\TFD\Extension.php
....
\sites\all\libraries\Twig\
\sites\all\libraries\Twig\lib\
\sites\all\libraries\Twig\lib\Twig\
\sites\all\libraries\Twig\lib\Twig\Node\
\sites\all\libraries\Twig\lib\Environment.php
....
\sites\all\themes\
\sites\all\themes\engines\
\sites\all\themes\engines\twig\
\sites\all\themes\engines\twig\twig.engine

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.

Configuration

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.

Filters

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.

Example

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;
}

Functions

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.

Tests

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');
}

Project Information