SlideShare una empresa de Scribd logo
1 de 39
Descargar para leer sin conexión
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

Más contenido relacionado

Similar a Twig in drupal8

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
Valeriy Kravchuk
 
Turku loves-storybook-styleguidist-styled-components
Turku loves-storybook-styleguidist-styled-componentsTurku loves-storybook-styleguidist-styled-components
Turku loves-storybook-styleguidist-styled-components
James Stone
 
Rules Programming tutorial
Rules Programming tutorialRules Programming tutorial
Rules Programming tutorial
Srinath Perera
 

Similar 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

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Último (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

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