Twig
Drupal 8 is upon us and with it comes the Twigification of all theme output. Anything that was a PHP template or theme function in Drupal 7 is now a Twig template so we all need to level up on our Twiggy knowledge—and I'm not talking about this Twiggy or this Twiggy.
Syntax
PrintingÂ
{{ someVar }}
Comments
{# This is a comment, yo. #}
Control Structures
{% if var %} ... {% endif %}
{% for user in users if user.active %} {{ user.name }} : {{ user.email }} {% else %} {# No active users #} {% endfor %}
Documentation
From the creators:Â
- http://twig.sensiolabs.org/
- http://twig.sensiolabs.org/documentation
- http://twig.sensiolabs.org/doc/templates.html Good outline of syntax and semantics
Drupal specific:
- Twig theming overview:Â https://www.drupal.org/docs/8/theming/twig
- Working With Twig Templates:Â https://www.drupal.org/docs/8/theming/twig/working-with-twig-templates
- Twig Template naming conventions:Â https://www.drupal.org/docs/8/theming/twig/twig-template-naming-conventions
- Comparison of PHPTemplate and Twig theming paradigms:Â https://www.drupal.org/docs/8/theming/twig/comparison-of-phptemplate-and-twig-theming-paradigms
- Discovering and Inspecting Variables in Twig Templates:Â https://www.drupal.org/docs/8/theming/twig/discovering-and-inspecting-variables-in-twig-templates
- Debugging Twig templates:Â https://www.drupal.org/docs/8/theming/twig/debugging-twig-templates
- Debugging compiled Twig templates https://www.drupal.org/docs/8/theming/twig/debugging-compiled-twig-templates
- Locating Template Files with Debugging:Â https://www.drupal.org/docs/8/theming/twig/locating-template-files-with-debugging
- Filters - Modifying Variables In Twig Templates:Â https://www.drupal.org/docs/8/theming/twig/filters-modifying-variables-in-twig-templates
- Functions - In Twig Templates:Â https://www.drupal.org/docs/8/theming/twig/functions-in-twig-templates
- Macros in Twig Templates:Â https://www.drupal.org/docs/8/theming/twig/macros-in-twig-templates
- Twig best practices - preprocess functions and templates: https://www.drupal.org/docs/8/theming/twig/twig-best-practices-preprocess-functions-and-templates
- Create custom twig templates from custom module:Â https://www.drupal.org/docs/8/theming/twig/create-custom-twig-templates-from-custom-moduleÂ
- Drupal specific twig filters https://www.drupal.org/node/2357633
Tutorials:
- Not Drupal specific despite being on Drupalize.me but fairly comprehensive outline of all the concepts.Â
https://drupalize.me/videos/twig-basics?p=1899 - Overview page with links to all the Drupal-specific Twig videos.
https://drupalize.me/tutorial/twig-drupal?p=2512
- Lots more in-depth and Drupal specific stuff here.
https://drupalize.me/search?query=twig
Dev Setup
- Atom
- Language support:Â https://github.com/reesef/php-twig
- Snippets:Â https://gist.github.com/soniktrooth/593722ae72a57cd1a5d06efaa8a09ce8