Twig for drupal, simple usage examples

These examples might be a little outdated.. Will rewrite into proper documentation soon…


As promised  some examples on how to use the twig-for-drupal engine. Part of these examples are written by Rik van der Kemp aka @cursief on twitter

After this the usage,and even the need for twig in Drupal will hopefully a bit more clear to you. Twig forces you into writing clean template code instead of using php logic because hrmm well you can ;)

Twig for readability

Normally you will write something like this, if you need to write a bunch of links to the screen

<?php foreach ($links as $link) : ?>
<a href="<?php print $link['href']; ?>" id="<?php print $link['id']; ?>" title="<?php print $link['title']; ?>" target="<?php print $link['target']; ?>"><?php print $link['description']; ?></a>
<?php endforeach; ?>

Not that hard to read, i admit that.. Now the same block in twig syntax

{% for link in links %}
{% endfor %}

Much easier on the eye, isn’t it :)
And for practical use, this is my current test block from garland translated into twig

<div id="block-{{block.module}}-{{}}" class="clear-block block block-{{block.module}}">
{% if (block.subject) %}
{% endif %}
  <div class="content">{{ block.content}}</div>

Objects in templates

Since in twig there is no difference between an array of a variable , you can call object methods very easily.
Say you  have a object called sheep defined that looks like this

class sheep(){
 function identify(){
	return "blaat";

And you assign this to a block in your drupal installation by using the twig_preprocess_block() implementation of template_preprocess_block()

function twig_block_preprocess(&$block){
 $block["sheep"] = new sheep();

Then in your twigged template, you want the sheep to identify itself by the noise it makes

The sheep is recognized by the {{sheep.identify()}} noise it makes.

Can you see where this is going? Imagine assigning views or form code to your template and simply call them in a much more cleanier and easier to read way, by keeping the logic where it belongs!

Translating and linking, and debugging.

By default twig-for-drupal comes with two wrappers for the most used functions in a template, or atleast i think so.

<a href="{%l 'node/12' %}">{%t 'readmore' %}</a>
Produce a translated readmore link to node/12 , and node/12 is nicelly SEO friendly if you are using pathauto.

Currently in the github repository there is also support for the devel module. If enabled, you can use it to either DPR or DPM your variables

<div id="block-{{block.module}}-{{}}" class="clear-block block block-{{block.module}}">
{% if (block.subject) %}
{% endif %}
  <div class="content">{% dpr block %}</div>

Please note that Twig removes the spaces between the {% dpr, is just use them to make it more readable.

Today’s changes

After working with the twig engine last week, two of my colleagues wanted to use doctrine generated objects for a D6 project currently in the making, but ran into some issues with the default twig behaviour of putting is_set() function around variables in compiled templates. Which gave some issues with not calling the proper __GET for those objects.

This is changed now, which means that the content of the variable is no longer checked in the compiled template and you NEED to either nicely assign/init everything you use or put a IF statement around it yourself. Later on, when the module to configure the twig engine is done, this will become an on/off option.

An other issue was naming of the cache files, by default with made a file called __TWIG_TEMPLATE__.MD5(‘templatename’).php.

This is changed into a more easier findable themename_templatename_tpl_html.php file in the /files/twig_cache folder.
Just in case you are left wondering what the *bleep* twig is doing underwater.

One more thing….

As from now, the engine gives a drupal_set_message warning if fallback mode is being used. So you can see which template’s you need to twig!

Happy twigging….. and grab the latest version from my github

Bookmark and Share

1 Comment

  1. Pit · 11/07/2012

    Nice tutorial, thanks

Leave a Reply