SlideShare a Scribd company logo
1 of 38
Download to read offline
TWIG
Drupal 8, It’s in Core

OCPHP / OC Drupal

@ocphp
ME
•

PHP Application Developer

Who happens to do Drupal

Who also likes to dabble in frontend	


•

Experience with Twig is in Silex, a micro-framework
built on Symfony	


•

Been organizer of OCPHP for nearly 8 years and OC
Drupal around ~4 years
THE PRESENTATION

A little less about Drupal and more just about Twig	

Picture and Demo free
WHAT IS TWIG
•

A PHP Template Engine to separate Presentation
Layer from the Controller/Model (PHP)	


•

Developed by same people behind Symfony; yes,
that’s in Drupal 8 Core too
COMMON COMPLAINTS
BUT PHP WAS JUST FINE, 	

WHY TWIG ?
•

Mixed PHP with HTML can be just plain sloppy and hard to read	


•

PHP still has to scan html documents looking for all those <?php
tags amongst the HTML	


•

Designers have too much power and can open security bugs in
the presentation layer with PHP	


•

Defining a function or filtering for theming was sloppy — no real
standard way
I DON’T WANT MORE BLOAT
•

PHP is definitely faster but Twig is safer	

As a developer, you control the PHP executed in a template. No possible
coding hacks like this in Templates … 


•



<?php $user = db_query(“SELECT n.nid FROM users WHERE uid = “.
$_GET[‘uid’].“)”; ?>	

•

Obviously a bit more execution time and memory but Twig has been around for
a while, tested, and aims to be fast ! Generated PHP will run through OPCode.	


•

Twig caches the generate PHP code so only changes are re-generated
Besides … In Drupal, Twig is the least of your
worries when it comes to bloat.
Rendered Arrays
SO WHAT ARE THE BENEFITS
BENEFITS
•

An extendable template engine	


•

Secure (can enable variable output escaping globally)	


•

It’s Unit Tested	


•

Great documentation and online resources	


•

Not coupled to Symfony so it has its own roadmap and
community
EVEN MORE BENEFITS
•

Easy to understand clean syntax	


•

Extend it and create your own filters or functions to give
designers to use	


•

Once you know it, can use it in other PHP frameworks outside of
Drupal	


•

Syntax support in several IDEs (PHPStorm)

http://blog.jetbrains.com/phpstorm/2013/06/twig-support-inphpstorm/
NOT ANOTHER SYNTAX
LEARN !
…STOP WHINING, IT’S PRETTY
EASY…
PRINT OUTPUT
<?php print $user[‘name’]; ?>	

!
!

{{ user.name }}
COMMENTS
depending on developers mood	

<?php // …. ?>	

<?php /* …. ?>	

!

{# Comment #}
FILTERS
<?php t(‘Welcome, ’ . $user[‘name’]); ?>	

!

{{ ‘Welcome, @name’|t({ ‘@name’: user.name }) }}
COMBINING FILTERS
<?php strtoupper(t(‘Welcome, ’ . $user[‘name’]);) ?>	

!

{{ ‘Welcome, @name’|t({ ‘@name’: user.name }|upper) }}
IF
<?php if (isset($user[‘name’])) { echo $user[‘name’] } else { echo
‘Who are you?’ }; ?>	

!

{% if user.name is defined %} 	

{{ user.name }} 	

{% else %} 	

Who are you? 	

{% endif %}	

!

Not testing, but following might work too	

{% user.name|default(‘Who are you’) %}
LOOPS
<?php foreach ( $users as $key => $user ) { print
$user[‘name’]; } ?>	

!

{% for key, user in users %}	

	

 {{ user.name }}	

{% endfor %}
CONTROL WHITESPACE
use - in output or function tags	

!

{{- user.name -}}	

!

{%- if user.name is defined -%}{%- endif -%}	

!
CALCULATIONS
<?php print $user[‘total’] + 1; ?>	

!

{{ users.total + 1 }}	

!
!
CONCAT / SET STRINGS
<?php print strtolower(‘’Greeting ‘ . $user[‘first’]); ?>	

!

{% set greeting = 'Hello ' %}	

{% set name = user.first %}	

!

{{ greeting ~ name|lower }} {# Hello bob #}	

!

{{ (greeting ~ name)|lower }} {# hello bob #}
EXPRESSIONS
{% if 1 not in [1, 2, 3] %}	

!

{% if 'cd' in 'abcde' %}	

!

{% if 'Bob' starts with 'B' %}	

!

{% if phone matches '{^[d.]+$}' %}	

!
http://twig.sensiolabs.org/doc/templates.html#twig-expressions
LOGIC WITH EXPRESSIONS
PHP: && || <>	

!

and	

or 	

not	

!

{% if user.name is not defined or user.name == ‘Bob’ %}	

!
http://twig.sensiolabs.org/doc/templates.html#twig-expressions
EXTENDING
Don’t duplicate the whole twig file just to change a single piece of
output. Extend it.	

!

{% extends ‘layout.html.twig’ %}	

!

or if layout = “layout.html.twig” was set as Twig Global/var	

!

{% extends layout %}
CONDITIONAL EXTENDS
(mobile variable is set in code and passed to template)	

!

{% extends mobile ? “mobile.html.twig" : “layout.html.twig” %}	

!
!
!
BLOCKS
Define content in blocks to allow for extending of templates !	

!

{% block sidebar %}	

…content…	

{% endblock %}	

!
!
OVERRIDING BLOCKS
The benefit of extending and defining blocks in a template.	

!

Any child template can override just that block content from
the parent template without having to change any other html !	

!
OVERRIDING BLOCKS
{% extends '/core/modules/system/tests/modules/twig_theme_test/
modules/twig_namespace_a/templates/test.html.twig' %}	

!

{% block content %}	

	

 I just overrode the content block in the parent template.	

{% endblock %}	

!
!
INCLUDE OTHER TEMPLATES
{% extends ‘layout.html.twig’ %}	

!

{% include ‘sidebar-left.html.twig’ %}	

!

{% block content %}	

	

 I just overrode the content block in the parent template.	

{% endblock %}	

!
!
!
WHAT ABOUT DEBUGGING?
DEBUG IN DRUPAL 8
In settings.php, uncomment ….	

!

# $settings['twig_debug'] = TRUE;	

# $settings['twig_auto_reload'] = TRUE;	

# $settings['twig_cache'] = FALSE;
DEBUG IN DRUPAL 8
!

{{ dump(user) }}	

!
!
!

Since Twig process the template into a generate PHP file, don’t
believe you can step through with XDebug and IDE like PHPStorm.
(??)
TWIG LOOKS GREAT !	

BUT NOW WHAT ?
!

It’s a huge undertaking to replace the theme layer and currently
still progressing but several templates have been converted:
views, base templates, etc.	

!

Twig Initiative ToDo List
!

https://groups.drupal.org/node/278968
D7 TO D8 TEMPLATES
Extensive work required but checkout Twigify	

!

Twigify is a module for converting Drupal 7 PHP Template
themes to Drupal 8 Twig themes. It was presented/demoed at
Drupalcon Portland (BOF) and at numerous US camp sessions,
and also in the Twig Lab at Drupalcon Prague.	

!

https://drupal.org/node/2099611	

!
THANKS !
Questions?
william.estrada@ocphp.org	

!

linkedin.com/in/webbywe
RESOURCES
•

On IRC, #drupal-twig	


•

(Do something about Drupal theming) http://jacine.net/post/19652705220/theme-system	


•

(YouTube DrupalCon Portland) http://www.youtube.com/watch?v=tPJ6LRJN0nw&feature=youtu.be	


•

(Twig) http://twig.sensiolabs.org/	


•

(Twiggy - sample template by Jen Lampton) https://github.com/jenlampton/twiggy/tree/master/templates	


•

(Twig in Drupal 8 slides) http://www.slideshare.net/Wizzlern/twig-in-drupal8	


•

(Twig support in PHPStorm) http://blog.jetbrains.com/phpstorm/2013/06/twig-support-in-phpstorm/	


•

(Twig coding standards) http://twig.sensiolabs.org/doc/coding_standards.html	


•

(Debugging in Twig) https://drupal.org/node/1906780	


•

(Current ToDo List) https://groups.drupal.org/node/278968	


•

(Drupal 8 and power of Twig) http://rootwork.org/blog/2013/05/drupal-8-power-twig-drupalcon-portland-featured-session	


•

(Twig Performance) http://www.appneta.com/blog/twig-performance/

More Related Content

What's hot

Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)Laura Scott
 
Converting (X)HTML/CSS template to Drupal 7 Theme
Converting (X)HTML/CSS template to Drupal 7 ThemeConverting (X)HTML/CSS template to Drupal 7 Theme
Converting (X)HTML/CSS template to Drupal 7 ThemeAdolfo Nasol
 
Being Dangerous with Twig
Being Dangerous with TwigBeing Dangerous with Twig
Being Dangerous with TwigRyan Weaver
 
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 PHPCesare D'Amico
 
Introduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingIntroduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingRobert Carr
 
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 2009Emma Jane Hogbin Westby
 
Grok Drupal (7) Theming
Grok Drupal (7) ThemingGrok Drupal (7) Theming
Grok Drupal (7) ThemingPINGV
 
HTML 5 Step By Step - Ebook
HTML 5 Step By Step - EbookHTML 5 Step By Step - Ebook
HTML 5 Step By Step - EbookScottperrone
 
WordPress Theme Development for Designers
WordPress Theme Development for DesignersWordPress Theme Development for Designers
WordPress Theme Development for Designerselliotjaystocks
 
Drupal 7 Theming - what's new
Drupal 7 Theming - what's newDrupal 7 Theming - what's new
Drupal 7 Theming - what's newMarek Sotak
 
PSD to a Drupal Theme (using a base theme)
PSD to a Drupal Theme (using a base theme)PSD to a Drupal Theme (using a base theme)
PSD to a Drupal Theme (using a base theme)kuydigital
 
Drupal 7 Theme System
Drupal 7 Theme SystemDrupal 7 Theme System
Drupal 7 Theme SystemPeter Arato
 
Adopt or hack - how to hack a theme in a Drupal way
Adopt or hack - how to hack a theme in a Drupal wayAdopt or hack - how to hack a theme in a Drupal way
Adopt or hack - how to hack a theme in a Drupal wayMarek Sotak
 
Advancing Your Custom Fields - WordCamp 2014
Advancing Your Custom Fields - WordCamp 2014Advancing Your Custom Fields - WordCamp 2014
Advancing Your Custom Fields - WordCamp 2014Carleton Web Services
 
Atomicant Drupal 6 Theming
Atomicant Drupal 6 ThemingAtomicant Drupal 6 Theming
Atomicant Drupal 6 ThemingMarek Sotak
 

What's hot (20)

Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
Grok Drupal (7) Theming (presented at DrupalCon San Francisco)
 
Converting (X)HTML/CSS template to Drupal 7 Theme
Converting (X)HTML/CSS template to Drupal 7 ThemeConverting (X)HTML/CSS template to Drupal 7 Theme
Converting (X)HTML/CSS template to Drupal 7 Theme
 
Being Dangerous with Twig
Being Dangerous with TwigBeing Dangerous with Twig
Being Dangerous with 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
 
Introduction to Drupal (7) Theming
Introduction to Drupal (7) ThemingIntroduction to Drupal (7) Theming
Introduction to Drupal (7) Theming
 
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
 
Grok Drupal (7) Theming
Grok Drupal (7) ThemingGrok Drupal (7) Theming
Grok Drupal (7) Theming
 
A look at Drupal 7 Theming
A look at Drupal 7 ThemingA look at Drupal 7 Theming
A look at Drupal 7 Theming
 
Drupal Front End PHP
Drupal Front End PHPDrupal Front End PHP
Drupal Front End PHP
 
HTML 5 Step By Step - Ebook
HTML 5 Step By Step - EbookHTML 5 Step By Step - Ebook
HTML 5 Step By Step - Ebook
 
WordPress Theme Development for Designers
WordPress Theme Development for DesignersWordPress Theme Development for Designers
WordPress Theme Development for Designers
 
Drupal 7 Theming - what's new
Drupal 7 Theming - what's newDrupal 7 Theming - what's new
Drupal 7 Theming - what's new
 
PSD to a Drupal Theme (using a base theme)
PSD to a Drupal Theme (using a base theme)PSD to a Drupal Theme (using a base theme)
PSD to a Drupal Theme (using a base theme)
 
Drupal 7 Theme System
Drupal 7 Theme SystemDrupal 7 Theme System
Drupal 7 Theme System
 
Adopt or hack - how to hack a theme in a Drupal way
Adopt or hack - how to hack a theme in a Drupal wayAdopt or hack - how to hack a theme in a Drupal way
Adopt or hack - how to hack a theme in a Drupal way
 
Git Makes Me Angry Inside
Git Makes Me Angry InsideGit Makes Me Angry Inside
Git Makes Me Angry Inside
 
Oop's in php
Oop's in php Oop's in php
Oop's in php
 
Php go vrooom!
Php go vrooom!Php go vrooom!
Php go vrooom!
 
Advancing Your Custom Fields - WordCamp 2014
Advancing Your Custom Fields - WordCamp 2014Advancing Your Custom Fields - WordCamp 2014
Advancing Your Custom Fields - WordCamp 2014
 
Atomicant Drupal 6 Theming
Atomicant Drupal 6 ThemingAtomicant Drupal 6 Theming
Atomicant Drupal 6 Theming
 

Similar to Twig for Drupal 8 and PHP | Presented at OC Drupal

Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesShabir Ahmad
 
Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!Ryan Weaver
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practicesmanugoel2003
 
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroJoomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroSteven Pignataro
 
Introduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC FrameworksIntroduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC FrameworksGerald Krishnan
 
Drupal7 Theming session on the occassion of Drupal7 release party in Delhi NCR
Drupal7 Theming session on the occassion of  Drupal7 release party in Delhi NCRDrupal7 Theming session on the occassion of  Drupal7 release party in Delhi NCR
Drupal7 Theming session on the occassion of Drupal7 release party in Delhi NCRGaurav Mishra
 
Survey on Script-based languages to write a Chatbot
Survey on Script-based languages to write a ChatbotSurvey on Script-based languages to write a Chatbot
Survey on Script-based languages to write a ChatbotNguyen Giang
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPtutorialsruby
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPtutorialsruby
 
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008Mir Nazim
 
Drupal7 Release Party in Luxembourg
Drupal7 Release Party in LuxembourgDrupal7 Release Party in Luxembourg
Drupal7 Release Party in Luxembourgnvisionagency
 
Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Maurizio Pelizzone
 
Save time by applying clean code principles
Save time by applying clean code principlesSave time by applying clean code principles
Save time by applying clean code principlesEdorian
 
Future proofing design work with Web components
Future proofing design work with Web componentsFuture proofing design work with Web components
Future proofing design work with Web componentsbtopro
 

Similar to Twig for Drupal 8 and PHP | Presented at OC Drupal (20)

Drupal 8 - Core and API Changes
Drupal 8 - Core and API ChangesDrupal 8 - Core and API Changes
Drupal 8 - Core and API Changes
 
Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!Twig: Friendly Curly Braces Invade Your Templates!
Twig: Friendly Curly Braces Invade Your Templates!
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven PignataroJoomla! Day Chicago 2011 Presentation - Steven Pignataro
Joomla! Day Chicago 2011 Presentation - Steven Pignataro
 
Introduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC FrameworksIntroduction to Using PHP & MVC Frameworks
Introduction to Using PHP & MVC Frameworks
 
Drupal7 Theming session on the occassion of Drupal7 release party in Delhi NCR
Drupal7 Theming session on the occassion of  Drupal7 release party in Delhi NCRDrupal7 Theming session on the occassion of  Drupal7 release party in Delhi NCR
Drupal7 Theming session on the occassion of Drupal7 release party in Delhi NCR
 
Survey on Script-based languages to write a Chatbot
Survey on Script-based languages to write a ChatbotSurvey on Script-based languages to write a Chatbot
Survey on Script-based languages to write a Chatbot
 
Php
PhpPhp
Php
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHP
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHP
 
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
Best Practices For Drupal Developers By Mir Nazim @ Drupal Camp India 2008
 
FLossEd-BK Tequila Framework3.2.1
FLossEd-BK Tequila Framework3.2.1FLossEd-BK Tequila Framework3.2.1
FLossEd-BK Tequila Framework3.2.1
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
php 1
php 1php 1
php 1
 
Drupal7 Release Party in Luxembourg
Drupal7 Release Party in LuxembourgDrupal7 Release Party in Luxembourg
Drupal7 Release Party in Luxembourg
 
Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress
 
Save time by applying clean code principles
Save time by applying clean code principlesSave time by applying clean code principles
Save time by applying clean code principles
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Phphacku iitd
Phphacku iitdPhphacku iitd
Phphacku iitd
 
Future proofing design work with Web components
Future proofing design work with Web componentsFuture proofing design work with Web components
Future proofing design work with Web components
 

Recently uploaded

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
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 WorkerThousandEyes
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 

Recently uploaded (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
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
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 

Twig for Drupal 8 and PHP | Presented at OC Drupal

  • 1. TWIG Drupal 8, It’s in Core OCPHP / OC Drupal @ocphp
  • 2. ME • PHP Application Developer
 Who happens to do Drupal
 Who also likes to dabble in frontend • Experience with Twig is in Silex, a micro-framework built on Symfony • Been organizer of OCPHP for nearly 8 years and OC Drupal around ~4 years
  • 3. THE PRESENTATION A little less about Drupal and more just about Twig Picture and Demo free
  • 4. WHAT IS TWIG • A PHP Template Engine to separate Presentation Layer from the Controller/Model (PHP) • Developed by same people behind Symfony; yes, that’s in Drupal 8 Core too
  • 6. BUT PHP WAS JUST FINE, WHY TWIG ? • Mixed PHP with HTML can be just plain sloppy and hard to read • PHP still has to scan html documents looking for all those <?php tags amongst the HTML • Designers have too much power and can open security bugs in the presentation layer with PHP • Defining a function or filtering for theming was sloppy — no real standard way
  • 7. I DON’T WANT MORE BLOAT • PHP is definitely faster but Twig is safer As a developer, you control the PHP executed in a template. No possible coding hacks like this in Templates … 
 • 
 <?php $user = db_query(“SELECT n.nid FROM users WHERE uid = “. $_GET[‘uid’].“)”; ?> • Obviously a bit more execution time and memory but Twig has been around for a while, tested, and aims to be fast ! Generated PHP will run through OPCode. • Twig caches the generate PHP code so only changes are re-generated
  • 8. Besides … In Drupal, Twig is the least of your worries when it comes to bloat. Rendered Arrays
  • 9. SO WHAT ARE THE BENEFITS
  • 10. BENEFITS • An extendable template engine • Secure (can enable variable output escaping globally) • It’s Unit Tested • Great documentation and online resources • Not coupled to Symfony so it has its own roadmap and community
  • 11. EVEN MORE BENEFITS • Easy to understand clean syntax • Extend it and create your own filters or functions to give designers to use • Once you know it, can use it in other PHP frameworks outside of Drupal • Syntax support in several IDEs (PHPStorm)
 http://blog.jetbrains.com/phpstorm/2013/06/twig-support-inphpstorm/
  • 13. …STOP WHINING, IT’S PRETTY EASY…
  • 14. PRINT OUTPUT <?php print $user[‘name’]; ?> ! ! {{ user.name }}
  • 15. COMMENTS depending on developers mood <?php // …. ?> <?php /* …. ?> ! {# Comment #}
  • 16. FILTERS <?php t(‘Welcome, ’ . $user[‘name’]); ?> ! {{ ‘Welcome, @name’|t({ ‘@name’: user.name }) }}
  • 17. COMBINING FILTERS <?php strtoupper(t(‘Welcome, ’ . $user[‘name’]);) ?> ! {{ ‘Welcome, @name’|t({ ‘@name’: user.name }|upper) }}
  • 18. IF <?php if (isset($user[‘name’])) { echo $user[‘name’] } else { echo ‘Who are you?’ }; ?> ! {% if user.name is defined %} {{ user.name }} {% else %} Who are you? {% endif %} ! Not testing, but following might work too {% user.name|default(‘Who are you’) %}
  • 19. LOOPS <?php foreach ( $users as $key => $user ) { print $user[‘name’]; } ?> ! {% for key, user in users %} {{ user.name }} {% endfor %}
  • 20. CONTROL WHITESPACE use - in output or function tags ! {{- user.name -}} ! {%- if user.name is defined -%}{%- endif -%} !
  • 21. CALCULATIONS <?php print $user[‘total’] + 1; ?> ! {{ users.total + 1 }} ! !
  • 22. CONCAT / SET STRINGS <?php print strtolower(‘’Greeting ‘ . $user[‘first’]); ?> ! {% set greeting = 'Hello ' %} {% set name = user.first %} ! {{ greeting ~ name|lower }} {# Hello bob #} ! {{ (greeting ~ name)|lower }} {# hello bob #}
  • 23. EXPRESSIONS {% if 1 not in [1, 2, 3] %} ! {% if 'cd' in 'abcde' %} ! {% if 'Bob' starts with 'B' %} ! {% if phone matches '{^[d.]+$}' %} ! http://twig.sensiolabs.org/doc/templates.html#twig-expressions
  • 24. LOGIC WITH EXPRESSIONS PHP: && || <> ! and or not ! {% if user.name is not defined or user.name == ‘Bob’ %} ! http://twig.sensiolabs.org/doc/templates.html#twig-expressions
  • 25. EXTENDING Don’t duplicate the whole twig file just to change a single piece of output. Extend it. ! {% extends ‘layout.html.twig’ %} ! or if layout = “layout.html.twig” was set as Twig Global/var ! {% extends layout %}
  • 26. CONDITIONAL EXTENDS (mobile variable is set in code and passed to template) ! {% extends mobile ? “mobile.html.twig" : “layout.html.twig” %} ! ! !
  • 27. BLOCKS Define content in blocks to allow for extending of templates ! ! {% block sidebar %} …content… {% endblock %} ! !
  • 28. OVERRIDING BLOCKS The benefit of extending and defining blocks in a template. ! Any child template can override just that block content from the parent template without having to change any other html ! !
  • 29. OVERRIDING BLOCKS {% extends '/core/modules/system/tests/modules/twig_theme_test/ modules/twig_namespace_a/templates/test.html.twig' %} ! {% block content %} I just overrode the content block in the parent template. {% endblock %} ! !
  • 30. INCLUDE OTHER TEMPLATES {% extends ‘layout.html.twig’ %} ! {% include ‘sidebar-left.html.twig’ %} ! {% block content %} I just overrode the content block in the parent template. {% endblock %} ! ! !
  • 32. DEBUG IN DRUPAL 8 In settings.php, uncomment …. ! # $settings['twig_debug'] = TRUE; # $settings['twig_auto_reload'] = TRUE; # $settings['twig_cache'] = FALSE;
  • 33. DEBUG IN DRUPAL 8 ! {{ dump(user) }} ! ! ! Since Twig process the template into a generate PHP file, don’t believe you can step through with XDebug and IDE like PHPStorm. (??)
  • 34. TWIG LOOKS GREAT ! BUT NOW WHAT ? ! It’s a huge undertaking to replace the theme layer and currently still progressing but several templates have been converted: views, base templates, etc. ! Twig Initiative ToDo List ! https://groups.drupal.org/node/278968
  • 35. D7 TO D8 TEMPLATES Extensive work required but checkout Twigify ! Twigify is a module for converting Drupal 7 PHP Template themes to Drupal 8 Twig themes. It was presented/demoed at Drupalcon Portland (BOF) and at numerous US camp sessions, and also in the Twig Lab at Drupalcon Prague. ! https://drupal.org/node/2099611 !
  • 38. RESOURCES • On IRC, #drupal-twig • (Do something about Drupal theming) http://jacine.net/post/19652705220/theme-system • (YouTube DrupalCon Portland) http://www.youtube.com/watch?v=tPJ6LRJN0nw&feature=youtu.be • (Twig) http://twig.sensiolabs.org/ • (Twiggy - sample template by Jen Lampton) https://github.com/jenlampton/twiggy/tree/master/templates • (Twig in Drupal 8 slides) http://www.slideshare.net/Wizzlern/twig-in-drupal8 • (Twig support in PHPStorm) http://blog.jetbrains.com/phpstorm/2013/06/twig-support-in-phpstorm/ • (Twig coding standards) http://twig.sensiolabs.org/doc/coding_standards.html • (Debugging in Twig) https://drupal.org/node/1906780 • (Current ToDo List) https://groups.drupal.org/node/278968 • (Drupal 8 and power of Twig) http://rootwork.org/blog/2013/05/drupal-8-power-twig-drupalcon-portland-featured-session • (Twig Performance) http://www.appneta.com/blog/twig-performance/