SlideShare a Scribd company logo
Twig & Drupal
Frontend United Amsterdam 2012
About me
René Bakx (@renebakx http://renebakx.nl)

PHP Developer since 2000

user 590180 on drupal.org

Loves open source development

Hates the default HTML output of drupal
About Twig
Originally created by Armin Ronacher (Also
known for Jinja, a Python template engine)

Since 2009 part of Fabien Potencier’s world
domination plan aka Symfony2

Available as standalone package under a BSD
License
About Twig
Twig is a very modern PHP 5 based template
engine.

It is fast! Templates are compiled into PHP

It is secure! Templates can be sandboxed, output can
be escaped.

It is extensible! You override everything if you want.
About Twig
Object Oriented Templates!

It is for HTML, what LESS/SASS is for CSS.

Integrates seamless into many IDEs like :
Textmate, Sublime Text, Vim, Netbeans, PHP-
Storm, Eclipse, Coda and many others.
Twig for Drupal
Started about 2 years ago as a spare time proof of concept
project for @zichtonline.

Because we did not like PHPTemplate and the way
output is handled in Drupal

Despite being a d.o. sandbox project, it is very much
production ready!

Twig for Drupal (TFD), enforces separation of display
and pre/post-processing logic.
Twig 101
Template Logic
Template logic
{# Comment #}
{{ node.title }}
{{ node.taxonomy|join(‘, ‘) }}

{% for item in node.items %}
  {{ node.title }}
{% endfor %}

{% for i in range(0,3) %}
  - {{ i }} -
{% endfor %}
Template logic
{# twig template #}
{{ node.title }}


<?php
/* Array */
$node[‘title’]
/* Object */
$node->title
$node->title()
/* Render array of doom (tm) */
$node = array(‘#theme’ => ‘theme_title’,
                          ‘value => ‘My Title’))

Output that value of any of the following
PHP or Drupal types. Twig figures it out for
you :)
Template logic
 {{ node.taxonomy|join(‘, ‘) }}
 {{ node.taxonomy|join(‘, ‘)|title}}

 {% filter upper %}
 this text becomes uppercase
 {% endfilter %}




 Filters are use to modify variables or
 output. They can be chained with a |

@see http://twig.sensiolabs.org/doc/filters/index.html
Template logic
{% if user.id >= 1 %}
  {% for item in node.items %}
    {{ node.title }}
  {% endfor %}
{% endif %}




Control structures, called TAGS can be
used to control the flow of your output.

@see http://twig.sensiolabs.org/doc/tags/index.html
Template logic
{% for i in range(0,3) %}
  - {{ i }} -
{% endfor %}

{{ random(node.taxonomy.terms }}




Functions, can be used to generate content


@see http://twig.sensiolabs.org/doc/functions/index.html
Twig 102
Advanced logic
(aka the good stuff )
Nested loops
{% for node,comments in content.comments %}
    <h2>{{ loop.index }} - {{ node.title }}</h2>
    {% for comment in comments %}
        <h3>{{ comment.title }}</h3>
        <p>{{ comment.body }}</p>
    {% endfor %}
{% endfor %}
Template inheritance
{# page.tpl.twig #}

{% block header %}
  <header> .... </header>
{% endblock %}

{% block page %}
  <article> .... </article>
{% endblock %}

{% block footer %}
  <footer> .... </footer>
{% endblock %}
Template inheritance
{# page--404.tpl.twig #}
{% extends ‘page.tpl.twig’ %}

{% block page %}
  <h1>#fail!</h1>
  <p>Dude where’s my page?</p>
{% endblock %}




You only need to write the part that is
CHANGED. No need to duplicate code
between pages, nodes, blocks etc. etc.
Selective inheritance
{# page--mobile.tpl.twig #}
{% extends vars.mobi ? ‘mobile.tpl.twig’ : ‘page.tpl.twig’ %}
{% block page %}
  <article>......</article>
{% endblock %}




Allows you to define one base template for
mobile and one for other pages.

Just set the $mobi in page_preprocess() and
your done.
Dynamic includes
{% for block in content.blocks %}
  {% include ‘block_’ ~ block.name ~ '.twig.tpl' %}
{% endfor %}


{% include ‘block_’ ~ block.name|default('base') ~'.twig.tpl'




Write once, use multiple times by chunking
the parts of the code you use all over your
theme into includes instead of php
methods.
Dynamic includes
{% for block in content.blocks %}
  {% include ‘block_’ ~ block.name ~ '.twig.tpl' %}
{% endfor %}


{% include ‘block_’ ~ block.name|default('base') ~'.twig.tpl'




Write once, use multiple times by chunking
the parts of the code you use all over your
theme into includes instead of php
methods.
Macros
{# macro.form.tpl.twig #}

{% macro field(name, value, type, size) %}
    <input type="{{ type|default('text') }}"
           name="{{ name }}"
           value="{{ value|e }}"
           size="{{ size|default(20) }}">
{% endmacro %}

{% import ‘macro.form.tpl.twig’ as form %}
<p> {{form.field(‘password’,null,‘password’}} </p>



Macros are not PHP functions but re-usable
pieces of view logic.
And only can read the variables passed to the
macro.
Twig for Drupal7
(Drupal 6 is so 2008)
Installation
  The easiest way is using the drush make file from
  http://drupal.org/sandbox/ReneB/1528480

  Or download all components yourself
  (some assembling required)

Drupal       http://drupal.org/download

Twig         https://github.com/fabpot/Twig/

TFD          http://drupal.org/sandbox/ReneB/1075966
Your first Twig theme
name = twiggy
description = My first twig based theme
engine = twig
core = 7.x




But I need to convert all the existing
templates for all the modules every build
including core into .twig.tpl files first!
Your first Twig theme
name = twiggy
description = My first twig based theme
engine = twig
core = 7.x




But I need to convert all the existing

NOPE Not needed!
templates for all the modules every build
including core into .twig.tpl files first!
Your first Twig theme
 Drupal 7 is smart enough to detect wether
 a .tpl.twig exists and if not, it renders tpl.php

 You can even use a phpTemplate theme as base
 theme!
name = mothertwig
description = TWIGalized version of the mothership.
engine = twig
core = 7.x
base theme = mothership


some restrictions apply @see http://drupal.org/node/225125
Drupal addon WITH
{% with title as title, elements sandboxed %}
 <h1>{{title}}</h1>
 <div> {{elements}} </div>
{% endwith %}




Join elements into a new context and remove the
others from the view. Can be very useful to
make the array of doom a bit more readable.
Live Demo
(*crosses fingers*)

More Related Content

What's hot

Php through the eyes of a hoster pbc10
Php through the eyes of a hoster pbc10Php through the eyes of a hoster pbc10
Php through the eyes of a hoster pbc10
Combell NV
 
Php string function
Php string function Php string function
Php string function
Ravi Bhadauria
 
Session Server - Maintaing State between several Servers
Session Server - Maintaing State between several ServersSession Server - Maintaing State between several Servers
Session Server - Maintaing State between several Servers
Stephan Schmidt
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
P3 InfoTech Solutions Pvt. Ltd.
 
Object Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHPObject Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHP
RobertGonzalez
 
What Are Python Modules? Edureka
What Are Python Modules? EdurekaWhat Are Python Modules? Edureka
What Are Python Modules? Edureka
Edureka!
 
The Django Book chapter 4 templates (supplement)
The Django Book chapter 4 templates (supplement)The Django Book chapter 4 templates (supplement)
The Django Book chapter 4 templates (supplement)
Vincent Chien
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
Ian Macali
 
PHP Technical Questions
PHP Technical QuestionsPHP Technical Questions
PHP Technical Questions
Pankaj Jha
 

What's hot (11)

Php through the eyes of a hoster pbc10
Php through the eyes of a hoster pbc10Php through the eyes of a hoster pbc10
Php through the eyes of a hoster pbc10
 
Php string function
Php string function Php string function
Php string function
 
Session Server - Maintaing State between several Servers
Session Server - Maintaing State between several ServersSession Server - Maintaing State between several Servers
Session Server - Maintaing State between several Servers
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
 
Object Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHPObject Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHP
 
What Are Python Modules? Edureka
What Are Python Modules? EdurekaWhat Are Python Modules? Edureka
What Are Python Modules? Edureka
 
The Django Book chapter 4 templates (supplement)
The Django Book chapter 4 templates (supplement)The Django Book chapter 4 templates (supplement)
The Django Book chapter 4 templates (supplement)
 
lf-2003_01-0269
lf-2003_01-0269lf-2003_01-0269
lf-2003_01-0269
 
lab4_php
lab4_phplab4_php
lab4_php
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
PHP Technical Questions
PHP Technical QuestionsPHP Technical Questions
PHP Technical Questions
 

Similar to Twig for Drupal @ Frontendunited Amsterdam 2012

Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Maurizio Pelizzone
 
Drupal 8 - Corso frontend development
Drupal 8 - Corso frontend developmentDrupal 8 - Corso frontend development
Drupal 8 - Corso frontend development
sparkfabrik
 
Drupal 8: Theming
Drupal 8: ThemingDrupal 8: Theming
Drupal 8: Themingdrubb
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
manugoel2003
 
Building a Custom Theme in Drupal 8
Building a Custom Theme in Drupal 8Building a Custom Theme in Drupal 8
Building a Custom Theme in Drupal 8
Anne Tomasevich
 
Twig Brief, Tips&Tricks
Twig Brief, Tips&TricksTwig Brief, Tips&Tricks
Twig Brief, Tips&Tricks
Andrei Burian
 
Extending Twig
Extending TwigExtending Twig
Extending Twig
Gerry Vandermaesen
 
Drupal 8: frontend development
Drupal 8: frontend developmentDrupal 8: frontend development
Drupal 8: frontend development
sparkfabrik
 
One-hour Drupal 8 Theming
One-hour Drupal 8 ThemingOne-hour Drupal 8 Theming
One-hour Drupal 8 Theming
Mediacurrent
 
Powerful and flexible templates with Twig
Powerful and flexible templates with Twig Powerful and flexible templates with Twig
Powerful and flexible templates with Twig Michael Peacock
 
Twig in the wild august 2018 drupal govcon draft
Twig in the wild   august 2018 drupal govcon draftTwig in the wild   august 2018 drupal govcon draft
Twig in the wild august 2018 drupal govcon draft
JeremyKoulish
 
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
 
Drupal Themes
Drupal ThemesDrupal Themes
Drupal Themes
akosh
 
Tornadoweb
TornadowebTornadoweb
Tornadoweb
Osman Yuksel
 
Drupal7 themeing changes and inheritence
Drupal7 themeing changes and inheritenceDrupal7 themeing changes and inheritence
Drupal7 themeing changes and inheritence
Aimee Maree Forsstrom
 
Theming tips and tricks
Theming tips and tricksTheming tips and tricks
Theming tips and tricksaaroncouch
 
Drupal 8 introduction to theming
Drupal 8  introduction to themingDrupal 8  introduction to theming
Drupal 8 introduction to theming
Brahampal Singh
 
Twig internals - Maksym MoskvychevTwig internals maksym moskvychev
Twig internals - Maksym MoskvychevTwig internals   maksym moskvychevTwig internals - Maksym MoskvychevTwig internals   maksym moskvychev
Twig internals - Maksym MoskvychevTwig internals maksym moskvychev
DrupalCampDN
 
Thymeleaf and Spring Controllers.ppt
Thymeleaf and Spring Controllers.pptThymeleaf and Spring Controllers.ppt
Thymeleaf and Spring Controllers.ppt
Patiento Del Mar
 

Similar to Twig for Drupal @ Frontendunited Amsterdam 2012 (20)

Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress
 
Drupal 8 - Corso frontend development
Drupal 8 - Corso frontend developmentDrupal 8 - Corso frontend development
Drupal 8 - Corso frontend development
 
Drupal 8: Theming
Drupal 8: ThemingDrupal 8: Theming
Drupal 8: Theming
 
Drupal Best Practices
Drupal Best PracticesDrupal Best Practices
Drupal Best Practices
 
Building a Custom Theme in Drupal 8
Building a Custom Theme in Drupal 8Building a Custom Theme in Drupal 8
Building a Custom Theme in Drupal 8
 
Twig Brief, Tips&Tricks
Twig Brief, Tips&TricksTwig Brief, Tips&Tricks
Twig Brief, Tips&Tricks
 
Extending Twig
Extending TwigExtending Twig
Extending Twig
 
Drupal 8: frontend development
Drupal 8: frontend developmentDrupal 8: frontend development
Drupal 8: frontend development
 
One-hour Drupal 8 Theming
One-hour Drupal 8 ThemingOne-hour Drupal 8 Theming
One-hour Drupal 8 Theming
 
Powerful and flexible templates with Twig
Powerful and flexible templates with Twig Powerful and flexible templates with Twig
Powerful and flexible templates with Twig
 
Twig in the wild august 2018 drupal govcon draft
Twig in the wild   august 2018 drupal govcon draftTwig in the wild   august 2018 drupal govcon draft
Twig in the wild august 2018 drupal govcon draft
 
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
 
Drupal Themes
Drupal ThemesDrupal Themes
Drupal Themes
 
Drupal Front End PHP
Drupal Front End PHPDrupal Front End PHP
Drupal Front End PHP
 
Tornadoweb
TornadowebTornadoweb
Tornadoweb
 
Drupal7 themeing changes and inheritence
Drupal7 themeing changes and inheritenceDrupal7 themeing changes and inheritence
Drupal7 themeing changes and inheritence
 
Theming tips and tricks
Theming tips and tricksTheming tips and tricks
Theming tips and tricks
 
Drupal 8 introduction to theming
Drupal 8  introduction to themingDrupal 8  introduction to theming
Drupal 8 introduction to theming
 
Twig internals - Maksym MoskvychevTwig internals maksym moskvychev
Twig internals - Maksym MoskvychevTwig internals   maksym moskvychevTwig internals - Maksym MoskvychevTwig internals   maksym moskvychev
Twig internals - Maksym MoskvychevTwig internals maksym moskvychev
 
Thymeleaf and Spring Controllers.ppt
Thymeleaf and Spring Controllers.pptThymeleaf and Spring Controllers.ppt
Thymeleaf and Spring Controllers.ppt
 

Recently uploaded

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 

Recently uploaded (20)

Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 

Twig for Drupal @ Frontendunited Amsterdam 2012

  • 1. Twig & Drupal Frontend United Amsterdam 2012
  • 2. About me René Bakx (@renebakx http://renebakx.nl) PHP Developer since 2000 user 590180 on drupal.org Loves open source development Hates the default HTML output of drupal
  • 3. About Twig Originally created by Armin Ronacher (Also known for Jinja, a Python template engine) Since 2009 part of Fabien Potencier’s world domination plan aka Symfony2 Available as standalone package under a BSD License
  • 4. About Twig Twig is a very modern PHP 5 based template engine. It is fast! Templates are compiled into PHP It is secure! Templates can be sandboxed, output can be escaped. It is extensible! You override everything if you want.
  • 5. About Twig Object Oriented Templates! It is for HTML, what LESS/SASS is for CSS. Integrates seamless into many IDEs like : Textmate, Sublime Text, Vim, Netbeans, PHP- Storm, Eclipse, Coda and many others.
  • 6. Twig for Drupal Started about 2 years ago as a spare time proof of concept project for @zichtonline. Because we did not like PHPTemplate and the way output is handled in Drupal Despite being a d.o. sandbox project, it is very much production ready! Twig for Drupal (TFD), enforces separation of display and pre/post-processing logic.
  • 8. Template logic {# Comment #} {{ node.title }} {{ node.taxonomy|join(‘, ‘) }} {% for item in node.items %} {{ node.title }} {% endfor %} {% for i in range(0,3) %} - {{ i }} - {% endfor %}
  • 9. Template logic {# twig template #} {{ node.title }} <?php /* Array */ $node[‘title’] /* Object */ $node->title $node->title() /* Render array of doom (tm) */ $node = array(‘#theme’ => ‘theme_title’, ‘value => ‘My Title’)) Output that value of any of the following PHP or Drupal types. Twig figures it out for you :)
  • 10. Template logic {{ node.taxonomy|join(‘, ‘) }} {{ node.taxonomy|join(‘, ‘)|title}} {% filter upper %} this text becomes uppercase {% endfilter %} Filters are use to modify variables or output. They can be chained with a | @see http://twig.sensiolabs.org/doc/filters/index.html
  • 11. Template logic {% if user.id >= 1 %} {% for item in node.items %} {{ node.title }} {% endfor %} {% endif %} Control structures, called TAGS can be used to control the flow of your output. @see http://twig.sensiolabs.org/doc/tags/index.html
  • 12. Template logic {% for i in range(0,3) %} - {{ i }} - {% endfor %} {{ random(node.taxonomy.terms }} Functions, can be used to generate content @see http://twig.sensiolabs.org/doc/functions/index.html
  • 13. Twig 102 Advanced logic (aka the good stuff )
  • 14. Nested loops {% for node,comments in content.comments %} <h2>{{ loop.index }} - {{ node.title }}</h2> {% for comment in comments %} <h3>{{ comment.title }}</h3> <p>{{ comment.body }}</p> {% endfor %} {% endfor %}
  • 15. Template inheritance {# page.tpl.twig #} {% block header %} <header> .... </header> {% endblock %} {% block page %} <article> .... </article> {% endblock %} {% block footer %} <footer> .... </footer> {% endblock %}
  • 16. Template inheritance {# page--404.tpl.twig #} {% extends ‘page.tpl.twig’ %} {% block page %} <h1>#fail!</h1> <p>Dude where’s my page?</p> {% endblock %} You only need to write the part that is CHANGED. No need to duplicate code between pages, nodes, blocks etc. etc.
  • 17. Selective inheritance {# page--mobile.tpl.twig #} {% extends vars.mobi ? ‘mobile.tpl.twig’ : ‘page.tpl.twig’ %} {% block page %} <article>......</article> {% endblock %} Allows you to define one base template for mobile and one for other pages. Just set the $mobi in page_preprocess() and your done.
  • 18. Dynamic includes {% for block in content.blocks %} {% include ‘block_’ ~ block.name ~ '.twig.tpl' %} {% endfor %} {% include ‘block_’ ~ block.name|default('base') ~'.twig.tpl' Write once, use multiple times by chunking the parts of the code you use all over your theme into includes instead of php methods.
  • 19. Dynamic includes {% for block in content.blocks %} {% include ‘block_’ ~ block.name ~ '.twig.tpl' %} {% endfor %} {% include ‘block_’ ~ block.name|default('base') ~'.twig.tpl' Write once, use multiple times by chunking the parts of the code you use all over your theme into includes instead of php methods.
  • 20. Macros {# macro.form.tpl.twig #} {% macro field(name, value, type, size) %} <input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}"> {% endmacro %} {% import ‘macro.form.tpl.twig’ as form %} <p> {{form.field(‘password’,null,‘password’}} </p> Macros are not PHP functions but re-usable pieces of view logic. And only can read the variables passed to the macro.
  • 21. Twig for Drupal7 (Drupal 6 is so 2008)
  • 22. Installation The easiest way is using the drush make file from http://drupal.org/sandbox/ReneB/1528480 Or download all components yourself (some assembling required) Drupal http://drupal.org/download Twig https://github.com/fabpot/Twig/ TFD http://drupal.org/sandbox/ReneB/1075966
  • 23. Your first Twig theme name = twiggy description = My first twig based theme engine = twig core = 7.x But I need to convert all the existing templates for all the modules every build including core into .twig.tpl files first!
  • 24. Your first Twig theme name = twiggy description = My first twig based theme engine = twig core = 7.x But I need to convert all the existing NOPE Not needed! templates for all the modules every build including core into .twig.tpl files first!
  • 25. Your first Twig theme Drupal 7 is smart enough to detect wether a .tpl.twig exists and if not, it renders tpl.php You can even use a phpTemplate theme as base theme! name = mothertwig description = TWIGalized version of the mothership. engine = twig core = 7.x base theme = mothership some restrictions apply @see http://drupal.org/node/225125
  • 26. Drupal addon WITH {% with title as title, elements sandboxed %} <h1>{{title}}</h1> <div> {{elements}} </div> {% endwith %} Join elements into a new context and remove the others from the view. Can be very useful to make the array of doom a bit more readable.

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n