known for Jinja, a Python template engine) • Since 2009 part of Fabien Potencier’s world domination plan aka Symfony2 • Twig is a very modern PHP 5 based template engine. • It is fast! Templates are compiled into PHP • It is secure! Templates can be sandboxed, output can be escaped. • It is extensible! You override everything if you want. 3 Saturday, May 26, 12
for HTML, what LESS/SASS is for CSS. • Object Oriented Templates (DRY!) • Integrates seamless into many IDEs like : Textmate, Sublime Text, Vim, Netbeans, PHP- Storm, Eclipse, Coda and many others. 4 Saturday, May 26, 12
as a spare time proof of concept project for @zichtonline. • Because we did not like PHPTemplate and the way output is handled in Drupal • Enforces separation of display and pre/post- processing logic. • BUT.. it’s not the holy grail solution for the ‘array of doom’ problem! 5 Saturday, May 26, 12
%} this text becomes uppercase {% endfilter %} Filters are use to modify variables or output. They can be chained with a | @see http://twig.sensiolabs.org/doc/filters/index.html 9 Template logic 9 Saturday, May 26, 12
node.items %} {{ node.title }} {% endfor %} {% endif %} Control structures, called TAGS can be used to control the flow of your output. @see http://twig.sensiolabs.org/doc/tags/index.html 10 Template logic 10 Saturday, May 26, 12
- {% endfor %} {{ random(node.taxonomy.terms }} Functions, can be used to generate content @see http://twig.sensiolabs.org/doc/functions/index.html 11 Template logic 11 Saturday, May 26, 12
%} <h1>#fail!</h1> <p>Dude where’s my page?</p> {% endblock %} You only need to write the part that is CHANGED. No need to duplicate code between pages, nodes, blocks etc. etc. 15 Template inheritance (DRY!) 15 Saturday, May 26, 12
%} {% block page %} <article>......</article> {% endblock %} Allows you to define one base template for mobile and one for other pages. Just set the mobi in page_preprocess() and your done. 16 Selective inheritance 16 Saturday, May 26, 12
block.name ~ '.twig.tpl' %} {% endfor %} {% include ‘block_’ ~ block.name|default('base') ~'.twig.tpl' Write once, use multiple times by chunking the parts of the code you use all over your theme into includes instead of php methods. 17 Dynamic includes 17 Saturday, May 26, 12
<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}"> {% endmacro %} {% import ‘macro.form.tpl.twig’ as form %} <p> {{form.field(‘password’,null,‘password’}} </p> Macros are not PHP functions but re-usable pieces of view logic, but they only can use the variables passed to them NO GLOBALS. 18 Macros 18 Saturday, May 26, 12
title, elements sandboxed %} <h1>{{title}}</h1> <div> {{elements}} </div> {% endwith %} Join elements into a new context and remove the others from the view. Can be very useful to make the array of doom a bit more readable. 21 21 Saturday, May 26, 12
%} {% include 'header.tpl.html' %} {# no break needed, as this default behaviour #} {% case 'content' fallthrough %} {# do something and fall trough to next case #} {% endswitch %} A switch, case construct just like PHP has. Can be useful to prevent endless amounts of IF / ELSEIF constructions. 22 22 Saturday, May 26, 12
} function filter($value,$param){ /** $value contains the value the filter is called on $param contains a parameter of the filter } 24 {{ block.variable|yourfilter(‘param’) }} twig_getinstance()->addFilter(‘yourfilter’, new Twig_Filter_Function(‘filter’)); Using the hook_twig_filters() Or adding them directly Using them remains the same! 24 Saturday, May 26, 12
2.2Ghz with MAMP 2.0.2 with APC TWIG Compile Twig Render PHP Template 0 75 150 225 300 119 ms 130 ms 234 ms Template cache was deleted after the first hit, to rule out time to compile all other code into APC. 26 Saturday, May 26, 12
engine = twig core = 7.x But I need to convert all the existing templates for all the modules every build including core into .twig.tpl files first! 28 Your first Twig theme 28 Saturday, May 26, 12
engine = twig core = 7.x But I need to convert all the existing templates for all the modules every build including core into .twig.tpl files first! 29 Your first Twig theme 29 Saturday, May 26, 12
engine = twig core = 7.x But I need to convert all the existing templates for all the modules every build including core into .twig.tpl files first! NOPE Not needed! 29 Your first Twig theme 29 Saturday, May 26, 12
$themes[$key]- >info['engine']) { $themes[$key]->info['engine'] = $themes[$base_key]- >info['engine']; $themes[$key]->owner = $themes[$base_key]->owner; $themes[$key]->prefix = $themes[$base_key]->prefix; Issue is filed, but stuck in the queue @see http://drupal.org/node/1545964 30 • Drupal 7 is supposed to be smart enough to detect wether a .tpl.twig exists and if not, it renders tpl.php • And in most cases it is, except when you extend a phptemplate based theme. • Only then you need a simple core patch Your first Twig theme 30 Saturday, May 26, 12