SlideShare a Scribd company logo
1 of 41
Download to read offline
Twig
&
Drupal 8
Joonas Pajunen
• Fraktio Oy
• Symfony2 Developer
• Twitter: joonsp
Arto Iijalainen
• Druid Oy
• Drupal backend developer
• Drupal.org: Sir-Arturio
• Twitter: arto_iijalainen
Twig
Overview
• Twig in general
• Why Twig is in Drupal 8?
• Twig’s solutions to Drupal’s problems
• Current state of the project & conclusion
Templating engine
• Used in Symfony and other projects
• Twig is not a Symfony component
• Inspired by Django, originally by Armin Ronacher, via and maintained by Fabien Potencier
<body>
<ul id="navigation">
{% for item in navigation %}
<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
{% endfor %}
</ul>
<h1>My Webpage</h1>
{{ a_variable }}
{# <p> unimplemented feature </p> #}
</body>
Syntax
<body>
<ul id="navigation">
{% for item in navigation %}
<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
{% endfor %}
</ul>
<h1>My Webpage</h1>
{{ a_variable }}
{# <p> unimplemented feature </p> #}
</body>
• print {{ }}
Syntax
<body>
<ul id="navigation">
{% for item in navigation %}
<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
{% endfor %}
</ul>
<h1>My Webpage</h1>
{{ a_variable }}
{# <p> unimplemented feature </p> #}
</body>
• print {{ }}
• execute statements {% %}
Syntax
<body>
<ul id="navigation">
{% for item in navigation %}
<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
{% endfor %}
</ul>
<h1>My Webpage</h1>
{{ a_variable }}
{# <p> unimplemented feature </p> #}
</body>
• print {{ }}
• execute statements {% %}
• comment {# #}
Syntax
Language features
• control structures
• tests
• variables
• logic
• math
<ul>
{% for user in users %}
{% if loop.index is divisibleby(2) and loop.length < 2 %}
<li class=”even”>
{% else%}
<li class=”odd”>
{% endif %}
{{ user.username }}
</li>
{% else %}
<li>no users</li>
{% endfor %}
</ul>
Variable access
• array key
• object property
• getBar or isBar function
{{ foo.bar }}
Composability
• extends
• include
• use
• block
{# base.html.twig #}
<head>
{% block head %}
{% endblock %}
</head>
<body>
<div id="content">
{% block content %}{% endblock %}
</div>
</div>
{# page.html.twig #}
{% extends "base.html.twig" %}
{% block content %}
<h1>Index</h1>
<p class="important">
Welcome to my awesome homepage.
</p>
{% include "footer.html.twig" %}
{% endblock %}
Filters, functions
{{ post.published_at|date("m/d/Y") }}
{{ [“seppo”,“kimmo”]|first|upper }} -> SEPPO
{{ random(['apple', 'orange', 'citrus']) }}
Extensibility
• custom functions, tags, filters
• can override and customize syntax
class MoneyExtension extends Twig_Extension
{
    public function getFunctions() {
        return array(
            'cents_to_euros' => new Twig_Function_Method($this, 'centsToEuros')
        );
    }
    public function centsToEuros($cents) {
             return round($cents/100, 2);
    }
    public function getName() {
        return 'money';
    }
}
Example: creating a custom function
$twig = new Twig_Environment($loader);
$twig->addExtension(new MoneyExtension());
{{ money(250) }} €
becomes:
2.5 €
Example: creating a custom function (usage)
Speed
• parsed -> optimized php -> cached
• C-extension available
Documentation
• Extensive documentation available!
• http://twig.sensiolabs.org/documentation
Good IDE highlight support
• phpstorm
• netbeans
• eclipse
• sublime text
• notepad++
• vim
• textmate
• etc …
Why
D8
+ ?
New theme layer!
D8
Why do we need a new theme layer?
What’s wrong with D7’s theme layer?
or
http://www.smile-day.net/wp-content/uploads/2012/03/Winking-Smiley-Face1.jpg
Flaws in D7’s theme layer
• PHPtemplate is insecure
• Template language is Drupal-only (PHPtemplate + drupalisms)
• Lots of different ways to address variables
• Theme system is overly complex
• Two ways of creating markup: templates and theme functions
• Too many and too cluttered templates
PHPtemplate is insecure
<?php
db_query("DROP TABLE {node}");
unlink('sites/default/files/myfilename.pdf');
?>
PHPtemplate is insecure
<?php
db_query("DROP TABLE {node}");
unlink('sites/default/files/myfilename.pdf');
?>
Not possible in Twig! o/
Twig - Security
• autoescape by default
• sandbox
Template language is Drupal-only
Twig is proudly found elsewhere!
No need to reinvent the wheel.
Lots of different ways to address variables
• array key
• object property
• getBar or isBar function
{{ foo.bar }}
• $foo[‘bar’]
• $foo->bar
• $foo->getBar()
$foo->isBar()
PHPtemplate Twig
Theme system is overly complex
http://www.slideshare.net/nyccamp/a-new-theme-layer-for-drupal-8
Theme system is overly complex
http://www.slideshare.net/nyccamp/a-new-theme-layer-for-drupal-8
Two ways of creating markup
theme_admin_view();
admin-view.tpl.php
Only Twig templates left!
admin-view.html.twig
Too many and too cluttered templates
Templates will be cleaned and consolidated
in the refactoring process.
Flaws in D7’s theme layer
• PHPtemplate is insecure
• Template language is Drupal-only (PHPtemplate + drupalisms)
• Lots of different ways to address variables
• Theme system is overly complex
• Two ways of creating markup: templates and theme functions
• Too many and too cluttered templates
}
}
Twig will fix
Theme layer will fix
Current development status
• *DONE* Twig is now an official template engine in D8 core
• *IN PROGRESS* Templates are being converted on Twig
• *WAITING* Twig as a main template engine
• *IN PROGRESS* Markup refactoring
• … Creating Dream Markup
• … Rely on Twig auto-escape
• … Finish template suggenstions
• HELP NEEDED!
Conclusions …
Thanks!

More Related Content

What's hot

How does get template part works in twenty ten theme
How does get template part works in twenty ten themeHow does get template part works in twenty ten theme
How does get template part works in twenty ten theme
mohd rozani abd ghani
 
Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009
Emma Jane Hogbin Westby
 
SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014
Mark Rackley
 

What's hot (20)

Javascript for the c# developer
Javascript for the c# developerJavascript for the c# developer
Javascript for the c# developer
 
Building mobile applications with DrupalGap
Building mobile applications with DrupalGapBuilding mobile applications with DrupalGap
Building mobile applications with DrupalGap
 
WordPress Theme Development: Part 2
WordPress Theme Development: Part 2WordPress Theme Development: Part 2
WordPress Theme Development: Part 2
 
Challenges going mobile
Challenges going mobileChallenges going mobile
Challenges going mobile
 
WordPress Theme Development for Designers
WordPress Theme Development for DesignersWordPress Theme Development for Designers
WordPress Theme Development for Designers
 
Plone 5 theming
Plone 5 themingPlone 5 theming
Plone 5 theming
 
Why Django
Why DjangoWhy Django
Why Django
 
URUG Ruby on Rails Workshop - Sesssion 5
URUG Ruby on Rails Workshop - Sesssion 5URUG Ruby on Rails Workshop - Sesssion 5
URUG Ruby on Rails Workshop - Sesssion 5
 
How does get template part works in twenty ten theme
How does get template part works in twenty ten themeHow does get template part works in twenty ten theme
How does get template part works in twenty ten theme
 
Drupal 7 Theme System
Drupal 7 Theme SystemDrupal 7 Theme System
Drupal 7 Theme System
 
Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009Learning PHP for Drupal Theming, DC Chicago 2009
Learning PHP for Drupal Theming, DC Chicago 2009
 
Create responsive websites with Django, REST and AngularJS
Create responsive websites with Django, REST and AngularJSCreate responsive websites with Django, REST and AngularJS
Create responsive websites with Django, REST and AngularJS
 
SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014SharePoint & jQuery Guide - SPSNashville 2014
SharePoint & jQuery Guide - SPSNashville 2014
 
The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017The Way to Theme Enlightenment 2017
The Way to Theme Enlightenment 2017
 
Git Makes Me Angry Inside
Git Makes Me Angry InsideGit Makes Me Angry Inside
Git Makes Me Angry Inside
 
What's new in D7 Theming?
What's new in D7 Theming?What's new in D7 Theming?
What's new in D7 Theming?
 
Hybrid Web Applications
Hybrid Web ApplicationsHybrid Web Applications
Hybrid Web Applications
 
Drupal theming - a practical approach (European Drupal Days 2015)
Drupal theming - a practical approach (European Drupal Days 2015)Drupal theming - a practical approach (European Drupal Days 2015)
Drupal theming - a practical approach (European Drupal Days 2015)
 
Writing a WordPress Theme - HighEdWeb 2013 #WRK2
Writing a WordPress Theme - HighEdWeb 2013 #WRK2Writing a WordPress Theme - HighEdWeb 2013 #WRK2
Writing a WordPress Theme - HighEdWeb 2013 #WRK2
 
Django Overview
Django OverviewDjango Overview
Django Overview
 

Viewers also liked

Viewers also liked (7)

Drupal 8 introduction to theming
Drupal 8  introduction to themingDrupal 8  introduction to theming
Drupal 8 introduction to theming
 
Drupal 8: Theming. Lviv Drupal Cafe #4
Drupal 8: Theming. Lviv Drupal Cafe #4Drupal 8: Theming. Lviv Drupal Cafe #4
Drupal 8: Theming. Lviv Drupal Cafe #4
 
Drupal8 theming
Drupal8 themingDrupal8 theming
Drupal8 theming
 
Drupal 8 theming
Drupal 8 themingDrupal 8 theming
Drupal 8 theming
 
Drupal8 themingdeepdive drupaldevdays-montpellier17042015
Drupal8 themingdeepdive drupaldevdays-montpellier17042015Drupal8 themingdeepdive drupaldevdays-montpellier17042015
Drupal8 themingdeepdive drupaldevdays-montpellier17042015
 
Ready. Set. Drupal! An Intro to Drupal 8, Part 2
Ready. Set. Drupal! An Intro to Drupal 8, Part 2Ready. Set. Drupal! An Intro to Drupal 8, Part 2
Ready. Set. Drupal! An Intro to Drupal 8, Part 2
 
[Srijan Wednesday Webinars] Drupal 8: Goodbye to 10 Years of Theming Headaches
[Srijan Wednesday Webinars] Drupal 8: Goodbye to 10 Years of Theming Headaches[Srijan Wednesday Webinars] Drupal 8: Goodbye to 10 Years of Theming Headaches
[Srijan Wednesday Webinars] Drupal 8: Goodbye to 10 Years of Theming Headaches
 

Similar to Twig

Twig, the flexible, fast, and secure template language for PHP
Twig, the flexible, fast, and secure template language for PHPTwig, the flexible, fast, and secure template language for PHP
Twig, the flexible, fast, and secure template language for PHP
Fabien Potencier
 
HTML5 & Front-end overview
HTML5 & Front-end overviewHTML5 & Front-end overview
HTML5 & Front-end overview
Ashish Mukherjee
 
Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress
Maurizio Pelizzone
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
Gavin Roy
 
GDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App EngineGDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App Engine
Yared Ayalew
 
Darwin web standards
Darwin web standardsDarwin web standards
Darwin web standards
Justin Avery
 
Freelancer Weapons of mass productivity
Freelancer Weapons of mass productivityFreelancer Weapons of mass productivity
Freelancer Weapons of mass productivity
Gregg Coppen
 

Similar to Twig (20)

Html5 Brown Bag
Html5 Brown BagHtml5 Brown Bag
Html5 Brown Bag
 
Twig, the flexible, fast, and secure template language for PHP
Twig, the flexible, fast, and secure template language for PHPTwig, the flexible, fast, and secure template language for PHP
Twig, the flexible, fast, and secure template language for PHP
 
HTML5, just another presentation :)
HTML5, just another presentation :)HTML5, just another presentation :)
HTML5, just another presentation :)
 
Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API Changes
 
Frontend for developers
Frontend for developersFrontend for developers
Frontend for developers
 
WordPress-Templates mit Twig erstellen - PHPUGFFM
WordPress-Templates mit Twig erstellen - PHPUGFFMWordPress-Templates mit Twig erstellen - PHPUGFFM
WordPress-Templates mit Twig erstellen - PHPUGFFM
 
HTML5 & Front-end overview
HTML5 & Front-end overviewHTML5 & Front-end overview
HTML5 & Front-end overview
 
Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress
 
2022 HTML5: The future is now
2022 HTML5: The future is now2022 HTML5: The future is now
2022 HTML5: The future is now
 
An Introduction to Tornado
An Introduction to TornadoAn Introduction to Tornado
An Introduction to Tornado
 
GDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App EngineGDG Addis - An Introduction to Django and App Engine
GDG Addis - An Introduction to Django and App Engine
 
Grok Drupal (7) Theming - 2011 Feb update
Grok Drupal (7) Theming - 2011 Feb updateGrok Drupal (7) Theming - 2011 Feb update
Grok Drupal (7) Theming - 2011 Feb update
 
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
 
Toutch Jquery Mobile
Toutch Jquery MobileToutch Jquery Mobile
Toutch Jquery Mobile
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
 
Intro to HTML
Intro to HTMLIntro to HTML
Intro to HTML
 
PHPConf-TW 2012 # Twig
PHPConf-TW 2012 # TwigPHPConf-TW 2012 # Twig
PHPConf-TW 2012 # Twig
 
Darwin web standards
Darwin web standardsDarwin web standards
Darwin web standards
 
Freelancer Weapons of mass productivity
Freelancer Weapons of mass productivityFreelancer Weapons of mass productivity
Freelancer Weapons of mass productivity
 
Html5 drupal7 with mandakini kumari(1)
Html5 drupal7 with mandakini kumari(1)Html5 drupal7 with mandakini kumari(1)
Html5 drupal7 with mandakini kumari(1)
 

Recently uploaded

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

Twig

  • 2. Joonas Pajunen • Fraktio Oy • Symfony2 Developer • Twitter: joonsp
  • 3. Arto Iijalainen • Druid Oy • Drupal backend developer • Drupal.org: Sir-Arturio • Twitter: arto_iijalainen
  • 5. Overview • Twig in general • Why Twig is in Drupal 8? • Twig’s solutions to Drupal’s problems • Current state of the project & conclusion
  • 6. Templating engine • Used in Symfony and other projects • Twig is not a Symfony component • Inspired by Django, originally by Armin Ronacher, via and maintained by Fabien Potencier
  • 7. <body> <ul id="navigation"> {% for item in navigation %} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor %} </ul> <h1>My Webpage</h1> {{ a_variable }} {# <p> unimplemented feature </p> #} </body> Syntax
  • 8. <body> <ul id="navigation"> {% for item in navigation %} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor %} </ul> <h1>My Webpage</h1> {{ a_variable }} {# <p> unimplemented feature </p> #} </body> • print {{ }} Syntax
  • 9. <body> <ul id="navigation"> {% for item in navigation %} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor %} </ul> <h1>My Webpage</h1> {{ a_variable }} {# <p> unimplemented feature </p> #} </body> • print {{ }} • execute statements {% %} Syntax
  • 10. <body> <ul id="navigation"> {% for item in navigation %} <li><a href="{{ item.href }}">{{ item.caption }}</a></li> {% endfor %} </ul> <h1>My Webpage</h1> {{ a_variable }} {# <p> unimplemented feature </p> #} </body> • print {{ }} • execute statements {% %} • comment {# #} Syntax
  • 11. Language features • control structures • tests • variables • logic • math
  • 12. <ul> {% for user in users %} {% if loop.index is divisibleby(2) and loop.length < 2 %} <li class=”even”> {% else%} <li class=”odd”> {% endif %} {{ user.username }} </li> {% else %} <li>no users</li> {% endfor %} </ul>
  • 13. Variable access • array key • object property • getBar or isBar function {{ foo.bar }}
  • 15. {# base.html.twig #} <head> {% block head %} {% endblock %} </head> <body> <div id="content"> {% block content %}{% endblock %} </div> </div> {# page.html.twig #} {% extends "base.html.twig" %} {% block content %} <h1>Index</h1> <p class="important"> Welcome to my awesome homepage. </p> {% include "footer.html.twig" %} {% endblock %}
  • 16. Filters, functions {{ post.published_at|date("m/d/Y") }} {{ [“seppo”,“kimmo”]|first|upper }} -> SEPPO {{ random(['apple', 'orange', 'citrus']) }}
  • 17. Extensibility • custom functions, tags, filters • can override and customize syntax
  • 18. class MoneyExtension extends Twig_Extension {     public function getFunctions() {         return array(             'cents_to_euros' => new Twig_Function_Method($this, 'centsToEuros')         );     }     public function centsToEuros($cents) {              return round($cents/100, 2);     }     public function getName() {         return 'money';     } } Example: creating a custom function
  • 19. $twig = new Twig_Environment($loader); $twig->addExtension(new MoneyExtension()); {{ money(250) }} € becomes: 2.5 € Example: creating a custom function (usage)
  • 20. Speed • parsed -> optimized php -> cached • C-extension available
  • 21. Documentation • Extensive documentation available! • http://twig.sensiolabs.org/documentation
  • 22. Good IDE highlight support • phpstorm • netbeans • eclipse • sublime text • notepad++ • vim • textmate • etc …
  • 25. Why do we need a new theme layer? What’s wrong with D7’s theme layer? or
  • 27. Flaws in D7’s theme layer • PHPtemplate is insecure • Template language is Drupal-only (PHPtemplate + drupalisms) • Lots of different ways to address variables • Theme system is overly complex • Two ways of creating markup: templates and theme functions • Too many and too cluttered templates
  • 28. PHPtemplate is insecure <?php db_query("DROP TABLE {node}"); unlink('sites/default/files/myfilename.pdf'); ?>
  • 29.
  • 30. PHPtemplate is insecure <?php db_query("DROP TABLE {node}"); unlink('sites/default/files/myfilename.pdf'); ?> Not possible in Twig! o/
  • 31. Twig - Security • autoescape by default • sandbox
  • 32. Template language is Drupal-only Twig is proudly found elsewhere! No need to reinvent the wheel.
  • 33. Lots of different ways to address variables • array key • object property • getBar or isBar function {{ foo.bar }} • $foo[‘bar’] • $foo->bar • $foo->getBar() $foo->isBar() PHPtemplate Twig
  • 34. Theme system is overly complex http://www.slideshare.net/nyccamp/a-new-theme-layer-for-drupal-8
  • 35. Theme system is overly complex http://www.slideshare.net/nyccamp/a-new-theme-layer-for-drupal-8
  • 36. Two ways of creating markup theme_admin_view(); admin-view.tpl.php Only Twig templates left! admin-view.html.twig
  • 37. Too many and too cluttered templates Templates will be cleaned and consolidated in the refactoring process.
  • 38. Flaws in D7’s theme layer • PHPtemplate is insecure • Template language is Drupal-only (PHPtemplate + drupalisms) • Lots of different ways to address variables • Theme system is overly complex • Two ways of creating markup: templates and theme functions • Too many and too cluttered templates } } Twig will fix Theme layer will fix
  • 39. Current development status • *DONE* Twig is now an official template engine in D8 core • *IN PROGRESS* Templates are being converted on Twig • *WAITING* Twig as a main template engine • *IN PROGRESS* Markup refactoring • … Creating Dream Markup • … Rely on Twig auto-escape • … Finish template suggenstions • HELP NEEDED!