Top Banner
Mastering Drupal 8’s Twig John Albin Wilkins November 24, 2017
25

Mastering Drupal 8’s Twig

Jan 29, 2018

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mastering Drupal 8’s Twig

MasteringDrupal 8’s Twig

John Albin WilkinsNovember 24, 2017

Page 2: Mastering Drupal 8’s Twig

Twighttp://twig.sensiolabs.org/

Twig for Template Designers https://twig.symfony.com/doc/1.x/templates.html

Page 3: Mastering Drupal 8’s Twig

DRUPAL 8

TWIG

Built-in Drupal templates use ~30% of the

available Twig features.

Drupal 8.4.2 has Twig 1.32.0

Page 4: Mastering Drupal 8’s Twig

Why Twig?

Page 5: Mastering Drupal 8’s Twig

Modern Syntax

<?phpprintvariable;?>

{{variable}}

Drupal 7:

Twig:

Page 6: Mastering Drupal 8’s Twig

Where's theperformance hit?

<?php if ($is_true): ?> <b>TRUE!</b> <?php endif; ?>

PHP parser/compiler has to context switch

Page 7: Mastering Drupal 8’s Twig

Twigcompiles templates

into PHP classes

Page 8: Mastering Drupal 8’s Twig

Twig syntax

{{variable}}

{#comment#}

{%command%}

Page 9: Mastering Drupal 8’s Twig

Variable drilling

{{variable.thing}}

{{page.primary_menu}}

{{comment.owner.anonymous}}

Page 10: Mastering Drupal 8’s Twig

This is how Twig resolves complex variables

<nav>{{ content.links }}</nav> !

$content['links']

$content->links

$content->links()

$content->getLinks()

$content->isLinks()

null

Twig tries all these alternatives and uses the first one that exists.

Page 11: Mastering Drupal 8’s Twig

Some Drupal variables names are special

!

$variables['site_slogan']['#markup'] = ... !

{{ site_slogan.#markup }}

core/themes/bartik/bartik.theme

This doesn't work because of the # character

{{ site_slogan['#markup'] }}

{{ attribute(site_slogan, '#markup') }}

Page 12: Mastering Drupal 8’s Twig

Improving Twig performance• Twig provides a PHP extension. • This extension only implements

the variable resolving logic. • See twig.sensiolabs.org/doc/

installation.html#installing-the-c-extension

EXPECTEDPERFORMANCE

INCREASE

15%

Page 13: Mastering Drupal 8’s Twig

Twig filters(added by Twig)

{{variable|escape('html')}}

{{variable|e('html')}}

Also accepts: 'js', 'css', 'url', 'html_attr'

{{variable|raw}}

collections: |keys|first|last|length

{{variable|date('Y-m-d')}}{{'now'|date(timezone=user.timezone)}}

http://twig.sensiolabs.org/doc/filters/index.html

Drupal

auto-escapes

by default!

Page 14: Mastering Drupal 8’s Twig

Twig filters(added by Drupal)

{{variable|safe_join(',')}}

{{variable|without('something')}}

{{variable|clean_class}}{{variable|clean_id}}

{{variable|format_date('medium')}}

{{'Somestring'|t}}

https://www.drupal.org/node/2357633

Page 15: Mastering Drupal 8’s Twig

Translation

{{'astring'|t}}{{'astring'|trans}}

{%trans%}Submittedby{{author_name}}on{{date}}{%endtrans%}

Page 16: Mastering Drupal 8’s Twig

Attribute object{{attributes.addClass('foo')}}

{{attributes.removeClass('bar')}}

{%ifattributes.hasClass('thing')%}

{{attributes.setAttribute('id','thing')}}

{{attributes.removeAttribute('id')}}

https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Template!Attribute.php/class/Attribute/8

Page 17: Mastering Drupal 8’s Twig

Control directives{%ifthing%}{%endif%}

{%foruserinusers%}...{%else%}Thereisnospoon.{%endfor%}

{%foritemincollectionifitem.published%}

Page 18: Mastering Drupal 8’s Twig

Control directives

{%forrowinimages|batch(3)%}<divclass="row">{%forimageinrow%}<divclass="image"><imgsrc=""alt=""></div>{%endfor%}</div>{%endfor%}

Page 19: Mastering Drupal 8’s Twig

Extends

{%extends"region.html.twig"%}

{%setattributes=attributes.addClass('clearfix')%}

bartik/templates/region--header.html.twig:

classy/templates/layout/region.html.twig:{%setclasses=['region','region-'~region|clean_class,]%}<div{{attributes.addClass(classes)}}>{{content}}</div>

Page 20: Mastering Drupal 8’s Twig

Extends

{%extends"block.html.twig"%}{%blockcontent%}<ahref="{{path('<front>')}}"rel="home"><imgsrc="{{site_logo}}"alt="{{'Home'|t}}"/></a>{%endblock%}

classy/templates/block/block--system-branding-block.html.twig:

classy/templates/block/block.html.twig:<div{{attributes.addClass(classes)}}><h2{{title_attributes}}>{{label}}</h2>{%blockcontent%}{{content}}{%endblock%}</div>

Page 21: Mastering Drupal 8’s Twig

Include

{%include'@my_theme/grid_3.twig'%}

{%include'@my_theme/grid_3.twig'with{var1:"anewvalue",var2:anotherVar,}only%}

Page 22: Mastering Drupal 8’s Twig

Embed

{%embed'@my_theme/grid_3.twig'%}{%blockcolumn1%}…blockcontents…{%endblock%}{%endembed%}

Page 23: Mastering Drupal 8’s Twig

Debugging

In sites/default, copy default.services.ymlto services.yml.

Look for these lines and change "false" to "true". twig.config:debug:false

https://www.drupal.org/node/1906392

Page 24: Mastering Drupal 8’s Twig

Libraries

(adding CSS & JS)

my_theme.libraries.yml:breadcrumb:css:component:components/breadcrumb/breadcrumb.css:{} dependencies:-my_theme/visually-hidden

https://www.drupal.org/node/2216195

Page 25: Mastering Drupal 8’s Twig

What else?

Drupal 8 Theme Guide: www.drupal.org/theme-guide/8