This document discusses Twig and its use as the template engine in Drupal 8. It provides an overview of Twig, why it was chosen for D8, and how it addresses issues with Drupal 7's theme layer. Key points covered include Twig's syntax, features like inheritance and filters, security advantages over PHPTemplate, and the current status of Twig integration in Drupal 8 core.
5. Overview
• Twig in general
• Why Twig is in Drupal 8?
• Twig’s solutions to Drupal’s problems
• Current state of the project & conclusion
Monday, August 26, 13
6. Templating engine
• Used in Symfony2 and other projects
• Twig is not a Symfony component
• Originates from the Python world (Django and Jinja Tempaltes)
• Originally by Armin Ronacher, today maintained by Fabien Potencier
Monday, August 26, 13
18. class MoneyExtension extends Twig_Extension
{
public function getFunctions() {
return array(
'cents_to_euros' => new Twig_Function_Method($this, 'centsToEuros')
);
}
public function centsToEuros($cents) {
return round($cents/100, 2);
}
public function getName() {
return 'money';
}
}
Example: creating a custom function
Monday, August 26, 13
19. $twig = new Twig_Environment($loader);
$twig->addExtension(new MoneyExtension());
{{ cents_to_euros(250) }} €
becomes:
2.5 €
Example: creating a custom function (usage)
Monday, August 26, 13
20. Speed
• Twig template -> optimized PHP -> rendered HTML
• Caching at different levels
• C-extension available
Monday, August 26, 13
27. Flaws in D7’s theme layer
• PHPtemplate is insecure
• Template language is Drupal-only (PHPtemplate + drupalisms)
• Lots of different ways to address variables
• Theme system is overly complex
• Two ways of creating markup: templates and theme functions
• Too many and too cluttered templates
Monday, August 26, 13
31. Twig - Security
• autoescape by default
• sandbox
Monday, August 26, 13
32. Template language is Drupal-only
Twig is proudly found elsewhere!
No need to reinvent the wheel.
Monday, August 26, 13
33. Lots of different ways to address variables
• array key
• object property
• getBar or isBar function
{{ foo.bar }}
• $foo[‘bar’]
• $foo->bar
• $foo->getBar()
$foo->isBar()
PHPtemplate Twig
Monday, August 26, 13
34. Theme system is overly complex
http://www.slideshare.net/nyccamp/a-new-theme-layer-for-drupal-8
Monday, August 26, 13
35. Theme system is overly complex
http://www.slideshare.net/nyccamp/a-new-theme-layer-for-drupal-8
Monday, August 26, 13
36. Two ways of creating markup
theme_admin_view();
admin-view.tpl.php
Only Twig templates left!
admin-view.html.twig
Monday, August 26, 13
37. Too many and too cluttered templates
Templates will be cleaned and consolidated
in the refactoring process.
Monday, August 26, 13
38. Flaws in D7’s theme layer
• PHPtemplate is insecure
• Template language is Drupal-only (PHPtemplate + drupalisms)
• Lots of different ways to address variables
• Theme system is overly complex
• Two ways of creating markup: templates and theme functions
• Too many and too cluttered templates
}
}
Twig will fix
Theme layer will fix
Monday, August 26, 13
39. Current development status
• *DONE* Twig is now an official template engine in D8 core
• *DONE* Templates are being converted on Twig
• *WAITING* Twig as a main template engine
• *IN PROGRESS* Markup refactoring
• … Creating Dream Markup
• … Rely on Twig auto-escape
• … Finish template suggenstions
• HELP NEEDED!
Monday, August 26, 13