SlideShare uma empresa Scribd logo
1 de 39
Baixar para ler offline
Twig for Drupal 8
‣ Introduction
‣ A bit of history
‣ Why change?
‣ Pros and cons
‣ Some examples
‣ Twig syntax
‣ Debugging twig
2
3
About Wizzlern
Wizzlern
‣ 100% Drupal
‣ Drupal training and consultancy
‣ Front-, backend-development
‣ Drupal introduction and editors
‣ System management
‣ Since 2009
‣ Involved with the Drupal community
4
Twig for Drupal 8
The overhaul of Drupal’s theme layer
5
A bit of history
‣ Dec. 2011: Frustrations over D7 theme
system boiled up (again)
‣ Feb./March 2012: Alternatives discussed
‣ April 2012: Code sprints
‣ June 2012: Twig engine landed in core
6
Why change?
‣ Inconsistent data structure
(Render Array, Markup, Wrappers)
‣ Many levels of processing ((pre)process,
(pre)render, theme-function or template)
‣ It’s so hard to make changes.
‣ Problem summary:
http://jacine.net/post/19652705220/theme-system
7
And the winner is...
‣ Twig is a modern template engine for PHP
‣ Fast, secure, flexible
8
Twig
Why Twig?
‣ Pros
‣ Mature, well-tested, used in other major
platforms too (Zend Framework, CodeIgniter,
CakePHP)
‣ Improved security
‣ Easy syntax
‣ Cons
‣ New language to lean, extra learning curve
‣ Migration required by module maintainers and
site builders
9
More improvements
‣ Cleaner templates
‣ Consistent method to override output
‣ Debugging built-in
‣ IDE support
‣ A bit quicker theme development (my
prediction)
10
And more...
‣ Performance: Minimal changes expected.
‣ Learning curve:
The easy got easier (overrides) but
the hard is still hard (Render Array).
‣ Object oriented
11
A few examples
12
Old: block.tpl.php
13
New: block.html.twig
14
Old: theme_breadcrumb()
15
New: breadcrumb.html.twig
16
17
Twig syntax
Syntax
‣ {{ ... }} Prints something
‣ Prints a variabele or the result of an
expression
‣ Prints text, but also Render Array content.
‣ {% ... %} Controls something
‣ Controls the execution of a template, e.g.
conditions and loops
‣ {# ... #} Does nothing
‣ Descriptions and comments
18
Functions
{{ example(...) }}
Twig functions:
‣ cycle()
‣ dump()
‣ join()
‣ ...
Drupal functions:
‣ hide()
‣ show()
19
‣ http://twig.sensiolabs.org/doc/functions/index.html
Filters
{{ ... | example }}
{{ ... | example(...) }}
Twig filters:
‣ first, last,
‣ lower, upper,
‣ raw, escape, ...
Drupal filters:
‣ t
20
‣ http://twig.sensiolabs.org/doc/filters/index.html
aggregator-feed-source.html.twig
21
node.html.twig
22
aggregator-item.html.twig
23
t filter
24
{{ 'Add new comment'|t }}
{{ 'Last checked: @time ago'|t({'@time': time}) }}
Control structures
if-else, for
25
if-else condition
26
{% if new is defined %}
<mark class="new">{{ new }}</mark>
{% endif %}
‣ {% if ... %}
‣ {% else %}
‣ {% endif %}
for loop
27
{% for item in breadcrumb %}
{% if loop.first %}
<li>{{ item }}</li>
{% elseif loop.last %}
<li>{{ item }}</li>
{% else %}
<li>{{ item }} » </li>
{% endif %}
{% endfor %}
‣ {% for ... in ... %}
‣ {% endfor %}
Debugging Twig
28
Debugging Twig
{{ dump(...) }}
‣ Twig options in settings.php:
‣ $settings['twig_debug']
‣ $settings['twig_auto_reload']
‣ $settings['twig_cache']
‣ About debugging variables in Twig templates:
http://drupal.org/node/1906780
29
settings.php
$settings['twig_debug'] = TRUE;
Output in DOM:
<!-- THEME DEBUG -->
<!-- CALL: theme('region') -->
<!-- FILE NAME SUGGESTIONS:
* region--content.html.twig
x region.html.twig
-->
‣ Only for templates used in your theme.
30
settings.php
$settings['twig_auto_reload'] = TRUE;
‣ Templates will be recompiled when called.
$settings['twig_cache'] = FALSE;
‣ Templates will not be cached. Usually
twig_auto_reload = TRUE is sufficient.
31
Template cache
32
Note: By default no read access to files/php.
Demo
33
Twig demo
‣ Debug options in
settings.php
‣ Debug output in
DOM
‣ Template override
‣ Modify node output
34
Some more
theming goodies
35
Theme layer changes
‣ Your preprocess functions now go into:
THEMENAME.theme file (#1967614)
‣ Options to override or delete CSS files
added by modules or base themes
(#1876600).
‣ More theme functions accept render
elements. e.g. theme_item_list(), l() (#891112,
#1987114)
36
D8 Mobile Initiative
‣ Responsive design
‣ Visual breakpoint editor
‣ Responsive tables
‣ <picture> element added
‣ jQuery 1.9 included
‣ IE8 no longer supported
‣ Improved CSS standards, incl. SMACCS
37
That’s all folks
‣ Twig documentation:
http://twig.sensiolabs.org/doc/templates.html
‣ Contribute to Drupal 8 twig development:
http://lb.cm/twig
‣ These slides:
http://wizzlern.nl/drupaljam2013-twig
38
This work is licensed under the Creative Commons
Attribution-NonCommercial-ShareAlike 3.0 Unported
License.To view a copy of this license, visit http://
creativecommons.org/licenses/by-nc-sa/3.0/.
39
http://wizzlern.nl
Twitter: @wizzlern

Mais conteúdo relacionado

Semelhante a Twig in drupal8

Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to DjangoJames Casey
 
Performance schema in_my_sql_5.6_pluk2013
Performance schema in_my_sql_5.6_pluk2013Performance schema in_my_sql_5.6_pluk2013
Performance schema in_my_sql_5.6_pluk2013Valeriy Kravchuk
 
Drupal jam 2017 services
Drupal jam 2017 servicesDrupal jam 2017 services
Drupal jam 2017 servicesErik Stielstra
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181Mahmoud Samir Fayed
 
Sql server performance tuning
Sql server performance tuningSql server performance tuning
Sql server performance tuningngupt28
 
Pure Sign Breakfast Presentations - Drupal FieldAPI
Pure Sign Breakfast Presentations - Drupal FieldAPIPure Sign Breakfast Presentations - Drupal FieldAPI
Pure Sign Breakfast Presentations - Drupal FieldAPIPure Sign
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionsaber tabatabaee
 
Drupal contrib module maintaining
Drupal contrib module maintainingDrupal contrib module maintaining
Drupal contrib module maintainingAndrii Podanenko
 
Troubleshooting .net core on linux
Troubleshooting .net core on linuxTroubleshooting .net core on linux
Troubleshooting .net core on linuxPavel Klimiankou
 
Free GitOps Workshop + Intro to Kubernetes & GitOps
Free GitOps Workshop + Intro to Kubernetes & GitOpsFree GitOps Workshop + Intro to Kubernetes & GitOps
Free GitOps Workshop + Intro to Kubernetes & GitOpsWeaveworks
 
Turku loves-storybook-styleguidist-styled-components
Turku loves-storybook-styleguidist-styled-componentsTurku loves-storybook-styleguidist-styled-components
Turku loves-storybook-styleguidist-styled-componentsJames Stone
 
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years laterSymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years laterHaehnchen
 
Sprint 49 review
Sprint 49 reviewSprint 49 review
Sprint 49 reviewManageIQ
 
Rules Programming tutorial
Rules Programming tutorialRules Programming tutorial
Rules Programming tutorialSrinath Perera
 
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataModeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataDaniel Bristot de Oliveira
 

Semelhante a Twig in drupal8 (20)

Introduction to Django
Introduction to DjangoIntroduction to Django
Introduction to Django
 
Performance schema in_my_sql_5.6_pluk2013
Performance schema in_my_sql_5.6_pluk2013Performance schema in_my_sql_5.6_pluk2013
Performance schema in_my_sql_5.6_pluk2013
 
Drupal jam 2017 services
Drupal jam 2017 servicesDrupal jam 2017 services
Drupal jam 2017 services
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181
 
Extending Twig
Extending TwigExtending Twig
Extending Twig
 
Sql server performance tuning
Sql server performance tuningSql server performance tuning
Sql server performance tuning
 
Mastering Drupal 8’s Twig
Mastering Drupal 8’s TwigMastering Drupal 8’s Twig
Mastering Drupal 8’s Twig
 
Gitops Hands On
Gitops Hands OnGitops Hands On
Gitops Hands On
 
Pure Sign Breakfast Presentations - Drupal FieldAPI
Pure Sign Breakfast Presentations - Drupal FieldAPIPure Sign Breakfast Presentations - Drupal FieldAPI
Pure Sign Breakfast Presentations - Drupal FieldAPI
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English version
 
Drupal contrib module maintaining
Drupal contrib module maintainingDrupal contrib module maintaining
Drupal contrib module maintaining
 
Zendcon zray
Zendcon zrayZendcon zray
Zendcon zray
 
Troubleshooting .net core on linux
Troubleshooting .net core on linuxTroubleshooting .net core on linux
Troubleshooting .net core on linux
 
Free GitOps Workshop + Intro to Kubernetes & GitOps
Free GitOps Workshop + Intro to Kubernetes & GitOpsFree GitOps Workshop + Intro to Kubernetes & GitOps
Free GitOps Workshop + Intro to Kubernetes & GitOps
 
Turku loves-storybook-styleguidist-styled-components
Turku loves-storybook-styleguidist-styled-componentsTurku loves-storybook-styleguidist-styled-components
Turku loves-storybook-styleguidist-styled-components
 
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years laterSymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
 
Sprint 49 review
Sprint 49 reviewSprint 49 review
Sprint 49 review
 
Rules Programming tutorial
Rules Programming tutorialRules Programming tutorial
Rules Programming tutorial
 
Network (Automation) eAcademy
Network (Automation) eAcademyNetwork (Automation) eAcademy
Network (Automation) eAcademy
 
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataModeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
 

Último

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 

Último (20)

Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 

Twig in drupal8

  • 1.
  • 2. Twig for Drupal 8 ‣ Introduction ‣ A bit of history ‣ Why change? ‣ Pros and cons ‣ Some examples ‣ Twig syntax ‣ Debugging twig 2
  • 4. Wizzlern ‣ 100% Drupal ‣ Drupal training and consultancy ‣ Front-, backend-development ‣ Drupal introduction and editors ‣ System management ‣ Since 2009 ‣ Involved with the Drupal community 4
  • 5. Twig for Drupal 8 The overhaul of Drupal’s theme layer 5
  • 6. A bit of history ‣ Dec. 2011: Frustrations over D7 theme system boiled up (again) ‣ Feb./March 2012: Alternatives discussed ‣ April 2012: Code sprints ‣ June 2012: Twig engine landed in core 6
  • 7. Why change? ‣ Inconsistent data structure (Render Array, Markup, Wrappers) ‣ Many levels of processing ((pre)process, (pre)render, theme-function or template) ‣ It’s so hard to make changes. ‣ Problem summary: http://jacine.net/post/19652705220/theme-system 7
  • 8. And the winner is... ‣ Twig is a modern template engine for PHP ‣ Fast, secure, flexible 8 Twig
  • 9. Why Twig? ‣ Pros ‣ Mature, well-tested, used in other major platforms too (Zend Framework, CodeIgniter, CakePHP) ‣ Improved security ‣ Easy syntax ‣ Cons ‣ New language to lean, extra learning curve ‣ Migration required by module maintainers and site builders 9
  • 10. More improvements ‣ Cleaner templates ‣ Consistent method to override output ‣ Debugging built-in ‣ IDE support ‣ A bit quicker theme development (my prediction) 10
  • 11. And more... ‣ Performance: Minimal changes expected. ‣ Learning curve: The easy got easier (overrides) but the hard is still hard (Render Array). ‣ Object oriented 11
  • 18. Syntax ‣ {{ ... }} Prints something ‣ Prints a variabele or the result of an expression ‣ Prints text, but also Render Array content. ‣ {% ... %} Controls something ‣ Controls the execution of a template, e.g. conditions and loops ‣ {# ... #} Does nothing ‣ Descriptions and comments 18
  • 19. Functions {{ example(...) }} Twig functions: ‣ cycle() ‣ dump() ‣ join() ‣ ... Drupal functions: ‣ hide() ‣ show() 19 ‣ http://twig.sensiolabs.org/doc/functions/index.html
  • 20. Filters {{ ... | example }} {{ ... | example(...) }} Twig filters: ‣ first, last, ‣ lower, upper, ‣ raw, escape, ... Drupal filters: ‣ t 20 ‣ http://twig.sensiolabs.org/doc/filters/index.html
  • 24. t filter 24 {{ 'Add new comment'|t }} {{ 'Last checked: @time ago'|t({'@time': time}) }}
  • 26. if-else condition 26 {% if new is defined %} <mark class="new">{{ new }}</mark> {% endif %} ‣ {% if ... %} ‣ {% else %} ‣ {% endif %}
  • 27. for loop 27 {% for item in breadcrumb %} {% if loop.first %} <li>{{ item }}</li> {% elseif loop.last %} <li>{{ item }}</li> {% else %} <li>{{ item }} » </li> {% endif %} {% endfor %} ‣ {% for ... in ... %} ‣ {% endfor %}
  • 29. Debugging Twig {{ dump(...) }} ‣ Twig options in settings.php: ‣ $settings['twig_debug'] ‣ $settings['twig_auto_reload'] ‣ $settings['twig_cache'] ‣ About debugging variables in Twig templates: http://drupal.org/node/1906780 29
  • 30. settings.php $settings['twig_debug'] = TRUE; Output in DOM: <!-- THEME DEBUG --> <!-- CALL: theme('region') --> <!-- FILE NAME SUGGESTIONS: * region--content.html.twig x region.html.twig --> ‣ Only for templates used in your theme. 30
  • 31. settings.php $settings['twig_auto_reload'] = TRUE; ‣ Templates will be recompiled when called. $settings['twig_cache'] = FALSE; ‣ Templates will not be cached. Usually twig_auto_reload = TRUE is sufficient. 31
  • 32. Template cache 32 Note: By default no read access to files/php.
  • 34. Twig demo ‣ Debug options in settings.php ‣ Debug output in DOM ‣ Template override ‣ Modify node output 34
  • 36. Theme layer changes ‣ Your preprocess functions now go into: THEMENAME.theme file (#1967614) ‣ Options to override or delete CSS files added by modules or base themes (#1876600). ‣ More theme functions accept render elements. e.g. theme_item_list(), l() (#891112, #1987114) 36
  • 37. D8 Mobile Initiative ‣ Responsive design ‣ Visual breakpoint editor ‣ Responsive tables ‣ <picture> element added ‣ jQuery 1.9 included ‣ IE8 no longer supported ‣ Improved CSS standards, incl. SMACCS 37
  • 38. That’s all folks ‣ Twig documentation: http://twig.sensiolabs.org/doc/templates.html ‣ Contribute to Drupal 8 twig development: http://lb.cm/twig ‣ These slides: http://wizzlern.nl/drupaljam2013-twig 38
  • 39. This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.To view a copy of this license, visit http:// creativecommons.org/licenses/by-nc-sa/3.0/. 39 http://wizzlern.nl Twitter: @wizzlern