17
Twig in Drupal

Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

Embed Size (px)

Citation preview

Page 1: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

Twig in Drupal

Page 2: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience 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

Page 3: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

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.

Page 4: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

Twig Syntax

‣ {{ say something }}

‣ {% do something %}

‣ {# comment on something #}

Page 5: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

Say Something in TwigPrint the value of a variable called label

Print the highlighted region of a page in Drupal

{{ label }}

{{ page.highlighted }}

Page 6: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

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 %}

Page 7: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

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

Page 8: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

Why Twig in Drupal?‣ Improved security ‣ Simpler, cleaner templates ‣ Language intended for templates

Page 9: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

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.

Page 10: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

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

Page 11: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

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

Page 12: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

Community Benefits‣ Wide adoption outside Drupal ‣ Broad support within Drupal community

Page 13: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

Challenges‣ Need to learn a new language ‣ PHP still required for preprocess functions ‣ Advanced themers will need both PHP and Twig

knowledge

Page 14: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

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

Page 15: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

Who needs to learn Twig?‣ Module developers ‣ Theme developers ‣ Front-end developers working with template files

Page 16: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

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/

Page 17: Twig in Drupal - drupalize.me in Drupal ‣ Describe the role that Twig plays in creating Drupal themes ‣ Explain how Twig impacts the theming experience in Drupal ‣ …

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