Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal...
Transcript of Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal...
Twig in Drupal
Twig in Drupal‣ Describe the role that Twig plays in creating Drupal
themes ‣ Explain how Twig impacts the theming experience in
Drupal ‣ Point out additional resources for learning Twig
What Is Twig?‣ Twig is a template engine for PHP ‣ A template engine allows an application or system
like Drupal to separate the concerns of functional "business" logic and the presentation or markup of the resulting data.
Twig Syntax
‣ {{ say something }}
‣ {% do something %}
‣ {# comment on something #}
Say Something in TwigPrint the value of a variable called label
Print the highlighted region of a page in Drupal
{{ label }}
{{ page.highlighted }}
Do Something in TwigIf the footer_fifth region exists, output it with this HTML:
{% if page.footer_fifth %} <div class="site-footer__bottom"> {{ page.footer_fifth }} </div>{% endif %}
Inheritance in Twig‣ extends keyword lets you “dress” a template in
another template’s markup ‣ block defines the customizable area where another
template’s code will be “dropped in” ‣ When a template uses extends, all markup is
surrounded by block tags to define the custom markup
Why Twig in Drupal?‣ Improved security ‣ Simpler, cleaner templates ‣ Language intended for templates
Improved Security‣ Twig introduces autoescaping and sanitizes all HTML
to prevent XSRF attacks. ‣ PHP will not be executed in a Twig file and will display
as plain text. ‣ Previous versions of Drupal executed any PHP and
made it hard to know what user input was escaped.
Simplified Syntax, Cleaner Code‣ Syntax for printing values inside an array or object is
the same ‣ render keyword no longer used ‣ More streamlined, consistent syntax for printing
variables ‣ Elimination of print and render keywords clears up
confusion, results in cleaner templates
Twig: A Language for Themers‣ Twig is specifically templating language ‣ Language is designed for template authors that are
markup experts, not PHP experts ‣ All Twig functionality is meant for use in template files ‣ PHP functionality goes way beyond what is needed
for a template ‣ Twig empowers themers with an easy-to-learn syntax
Community Benefits‣ Wide adoption outside Drupal ‣ Broad support within Drupal community
Challenges‣ Need to learn a new language ‣ PHP still required for preprocess functions ‣ Advanced themers will need both PHP and Twig
knowledge
Why learn Twig?‣ To output markup in a module or theme ‣ Theme functions no longer exist in Drupal 8 ‣ All markup output is determined by a Twig template ‣ Twig is the default template language in Drupal 8 ‣ Upgrading to Drupal 8? All tpl.php will need to be
converted to html.twig
Who needs to learn Twig?‣ Module developers ‣ Theme developers ‣ Front-end developers working with template files
Resources‣ Twig Templating series
• https://drupalize.me/videos/twig-basics‣ Theme Guide on drupal.org
• https://www.drupal.org/theme-guide/8/twig‣ Twig documentation
• http://twig.sensiolabs.org/
Recap‣ What Twig is and why it is a good idea for Drupal ‣ How Twig impacts the theming experience ‣ Who should learn Twig ‣ Where to find resources to learn more