SlideShare a Scribd company logo
1 of 49
Download to read offline
Twig Templating

John Rudolph Bautista
Software Engineer

The Twig logo is © Sensio Labs

October 26th 2013
PHP AS TEMPLATE
Why TWIG
WAS
born?
WAYBACK 2009
FABIEN potencier
- Symfony
- SwiftMailer
- Silex
- Pimple
- Twig
…. many more
https://github.com/fabpot
TWIG
The Twig logo is © Sensio Labs
Twig
- Provides Pattern Consistency
- Solves Complex Control Flow
- Template Inheritance & Macros
- Fully Extendable
Twig
- Well Documented
- Unit Tested
- Secure
- Fast (has a caching layer by default)
Syntax Highlighting
Installation
VIa Composer
VIA Composer

http://getcomposer.org/
VIA TARBALL OR ZIP
https://github.com/fabpot/Twig/tags
- Unpack
- Move files to your project folder
VIA GIT
https://github.com/fabpot/Twig/tags
git clone git://github.com/fabpot/Twig.git
USING TWIG API
Delimiters
{{ ... }}
- displays something

{% ... %}
- evaluates an expression (loops, tags, cond.)

{# ... #}
- comments code blocks
Twig Template FILE
Twig Template FILE
← Comment
Twig Template FILE
← FILTER
Twig Template FILE
← TAG
Twig Template FILE

← VARIABLE
Twig Template FILE

← FILTER WITH
ARGUments
THE BASICS
LITERALS
String = “Manong Johny”
Integer = 100000000
Float = 100000000.00
Array = [“pdaf”, “dap”]
Hash = {“key” : “value”}
Boolean = true/false
Null = null
SETTING VARIABLES
{% set str = 'value' %}
{% set arr = [1, 2, 3] %}
{% set hash = {'a' : 1, 'b': 2 } %}
{% set combine = ['a' : { 'b': 2 }] %}
PRINTING VARIABLES
{{ var }}
{{ var['elem'] }}
{{ var.elem }}
{{ var.prop }}
FILTERS
- Modifies a variable
- Uses “|” (pipe)
- Chainable
- Can accept arguments
FILTERS
{{ employee|title }}
Result : “Janet Napoles”
{{ tags|join(', ') }}
Result : “ph, rich, more fun”
MACROS
- acts like functions in regular
programming language
- default argument values are defined
by using the default() filter
- arguments are always optional
MACROS
ESCAPING
- escapes a string for safe insertion
into the final output
{{ var|escape }}
{{ var|e }}
{% autoescape %}
….
{% endautoescape %}
ESCAPING
Default Escape : {{ str|escape }}
<p>Hello World!</p>
JS Escape : {{ str|escape('js ') }}
x3Cpx3EHellox20Worldx21x3Cx2Fpx3E
CSS Escape : {{ str|escape('css ') }}
3C p3E Hello20 World21 3C 2F p3E
URL Escape: {{ str|escape('url') }}
%3Cp%3EHello%20World%21%3C%2Fp%3E
IF, ELSEIF, ELSE
FOR LOOP
- loop each item in a sequence
FOR LOOP
FOR WITH CONDITION
FOR LOOP VARIABLES
TEMPLATE
INHERITANCE
BLOCK
- are used for inheritance and act
as placeholders and replacements.
{% block content %}
…..
{% endblock %}
NAMED BLOCK END-TAGS
EXTENDS
- can be used to extend a template
from another one.
{% extends “layout.twig” %}
EXTENDS & BLOCK
EXTENDS & BLOCK
EXTENDS & BLOCK

{% block content %}

{% endblock content %}
INCLUDE
- includes a template and return
the rendered content of that file
into the current namespace.
INCLUDE
INCLUDE
DRUPAL

+

TWIG
Drupal 8 & Twig Road Map : http://lb.cm/twig
<THANK
YOU!

More Related Content

What's hot

Twigエクステンションの作り方
Twigエクステンションの作り方Twigエクステンションの作り方
Twigエクステンションの作り方
Katsuhiro Ogawa
 
オレオレSecurityバンドル作っちゃいました
オレオレSecurityバンドル作っちゃいましたオレオレSecurityバンドル作っちゃいました
オレオレSecurityバンドル作っちゃいました
Katsuhiro Ogawa
 
Symfony2 and Doctrine2 Integration
Symfony2 and Doctrine2 IntegrationSymfony2 and Doctrine2 Integration
Symfony2 and Doctrine2 Integration
Jonathan Wage
 
Creating and Maintaining WordPress Plugins
Creating and Maintaining WordPress PluginsCreating and Maintaining WordPress Plugins
Creating and Maintaining WordPress Plugins
Mark Jaquith
 

What's hot (20)

UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013
 
What's new with PHP7
What's new with PHP7What's new with PHP7
What's new with PHP7
 
Twigエクステンションの作り方
Twigエクステンションの作り方Twigエクステンションの作り方
Twigエクステンションの作り方
 
Zephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensionsZephir - A Wind of Change for writing PHP extensions
Zephir - A Wind of Change for writing PHP extensions
 
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
 
Php on the Web and Desktop
Php on the Web and DesktopPhp on the Web and Desktop
Php on the Web and Desktop
 
Design patterns in PHP
Design patterns in PHPDesign patterns in PHP
Design patterns in PHP
 
オレオレSecurityバンドル作っちゃいました
オレオレSecurityバンドル作っちゃいましたオレオレSecurityバンドル作っちゃいました
オレオレSecurityバンドル作っちゃいました
 
Python Templating Engine - Intro to Jinja
Python Templating Engine - Intro to JinjaPython Templating Engine - Intro to Jinja
Python Templating Engine - Intro to Jinja
 
PHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better CodePHP: 4 Design Patterns to Make Better Code
PHP: 4 Design Patterns to Make Better Code
 
Symfony2 and Doctrine2 Integration
Symfony2 and Doctrine2 IntegrationSymfony2 and Doctrine2 Integration
Symfony2 and Doctrine2 Integration
 
Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2Zend Framework Study@Tokyo #2
Zend Framework Study@Tokyo #2
 
My app is secure... I think
My app is secure... I thinkMy app is secure... I think
My app is secure... I think
 
New in php 7
New in php 7New in php 7
New in php 7
 
Building a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing StrategiesBuilding a Pyramid: Symfony Testing Strategies
Building a Pyramid: Symfony Testing Strategies
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
 
Creating and Maintaining WordPress Plugins
Creating and Maintaining WordPress PluginsCreating and Maintaining WordPress Plugins
Creating and Maintaining WordPress Plugins
 
Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3
 
Contributing to WordPress Core - Peter Wilson
Contributing to WordPress Core - Peter WilsonContributing to WordPress Core - Peter Wilson
Contributing to WordPress Core - Peter Wilson
 
Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010Symfony2 - WebExpo 2010
Symfony2 - WebExpo 2010
 

Viewers also liked

Plot - introduction
Plot - introductionPlot - introduction
Plot - introduction
kjoeper123
 
Presentación1 magi
Presentación1 magiPresentación1 magi
Presentación1 magi
mago_rdz77
 
conexion-ordenador-movil-presentacion
conexion-ordenador-movil-presentacionconexion-ordenador-movil-presentacion
conexion-ordenador-movil-presentacion
ccaballem
 
랭킹 최적화를 넘어 인간적인 검색으로 - 서울대 융합기술원 발표
랭킹 최적화를 넘어 인간적인 검색으로  - 서울대 융합기술원 발표랭킹 최적화를 넘어 인간적인 검색으로  - 서울대 융합기술원 발표
랭킹 최적화를 넘어 인간적인 검색으로 - 서울대 융합기술원 발표
Jin Young Kim
 
Input characters
Input charactersInput characters
Input characters
kjoeper123
 

Viewers also liked (20)

Input plot
Input plot Input plot
Input plot
 
Plot - introduction
Plot - introductionPlot - introduction
Plot - introduction
 
Presentación1 magi
Presentación1 magiPresentación1 magi
Presentación1 magi
 
conexion-ordenador-movil-presentacion
conexion-ordenador-movil-presentacionconexion-ordenador-movil-presentacion
conexion-ordenador-movil-presentacion
 
Rockefeller tsg meeting 27_may2012_fnl
Rockefeller tsg meeting 27_may2012_fnlRockefeller tsg meeting 27_may2012_fnl
Rockefeller tsg meeting 27_may2012_fnl
 
EVALUACION
EVALUACIONEVALUACION
EVALUACION
 
connexió windows 7-htc desire
connexió windows 7-htc desireconnexió windows 7-htc desire
connexió windows 7-htc desire
 
Pixelim Media - İnteraktif Çözümler
Pixelim Media - İnteraktif ÇözümlerPixelim Media - İnteraktif Çözümler
Pixelim Media - İnteraktif Çözümler
 
Sejarah malaysia
Sejarah malaysiaSejarah malaysia
Sejarah malaysia
 
Cross Slot technology
Cross Slot technologyCross Slot technology
Cross Slot technology
 
evaluacion
evaluacionevaluacion
evaluacion
 
SIGIR Tutorial on IR Evaluation: Designing an End-to-End Offline Evaluation P...
SIGIR Tutorial on IR Evaluation: Designing an End-to-End Offline Evaluation P...SIGIR Tutorial on IR Evaluation: Designing an End-to-End Offline Evaluation P...
SIGIR Tutorial on IR Evaluation: Designing an End-to-End Offline Evaluation P...
 
Input symbols
Input symbolsInput symbols
Input symbols
 
랭킹 최적화를 넘어 인간적인 검색으로 - 서울대 융합기술원 발표
랭킹 최적화를 넘어 인간적인 검색으로  - 서울대 융합기술원 발표랭킹 최적화를 넘어 인간적인 검색으로  - 서울대 융합기술원 발표
랭킹 최적화를 넘어 인간적인 검색으로 - 서울대 융합기술원 발표
 
Input plot
Input plot Input plot
Input plot
 
Digital Still Camera Market in India by Abhinava Mishra
Digital Still Camera Market in India by Abhinava MishraDigital Still Camera Market in India by Abhinava Mishra
Digital Still Camera Market in India by Abhinava Mishra
 
Input characters
Input charactersInput characters
Input characters
 
Subtleties in Tracking Happiness -- Seattle QS#10
Subtleties in Tracking Happiness -- Seattle QS#10Subtleties in Tracking Happiness -- Seattle QS#10
Subtleties in Tracking Happiness -- Seattle QS#10
 
Social Entrepreneur meets Technology by 황진솔 대표
Social Entrepreneur meets Technology by 황진솔 대표Social Entrepreneur meets Technology by 황진솔 대표
Social Entrepreneur meets Technology by 황진솔 대표
 
헬로 데이터 과학: 삶과 업무를 개선하는 데이터 과학 이야기 (스타트업 얼라이언스 강연)
헬로 데이터 과학: 삶과 업무를 개선하는 데이터 과학 이야기 (스타트업 얼라이언스 강연)헬로 데이터 과학: 삶과 업무를 개선하는 데이터 과학 이야기 (스타트업 얼라이언스 강연)
헬로 데이터 과학: 삶과 업무를 개선하는 데이터 과학 이야기 (스타트업 얼라이언스 강연)
 

Similar to Twig Templating

Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress
Maurizio Pelizzone
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus Presentation
Ajax Experience 2009
 

Similar to Twig Templating (20)

A Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to GoA Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to Go
 
Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress Use Symfony2 components inside WordPress
Use Symfony2 components inside WordPress
 
Media Frameworks Versus Swift (Swift by Northwest, October 2017)
Media Frameworks Versus Swift (Swift by Northwest, October 2017)Media Frameworks Versus Swift (Swift by Northwest, October 2017)
Media Frameworks Versus Swift (Swift by Northwest, October 2017)
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus Presentation
 
Let's play with adf 3.0
Let's play with adf 3.0Let's play with adf 3.0
Let's play with adf 3.0
 
Diseño y Desarrollo de APIs
Diseño y Desarrollo de APIsDiseño y Desarrollo de APIs
Diseño y Desarrollo de APIs
 
Sprockets
SprocketsSprockets
Sprockets
 
Os Secoske
Os SecoskeOs Secoske
Os Secoske
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Introduction to PowerShell
Introduction to PowerShellIntroduction to PowerShell
Introduction to PowerShell
 
Charla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo WebCharla EHU Noviembre 2014 - Desarrollo Web
Charla EHU Noviembre 2014 - Desarrollo Web
 
Camel_From_The_Field
Camel_From_The_FieldCamel_From_The_Field
Camel_From_The_Field
 
May The Nodejs Be With You
May The Nodejs Be With YouMay The Nodejs Be With You
May The Nodejs Be With You
 
DevOps(4) : Ansible(2) - (MOSG)
DevOps(4) : Ansible(2) - (MOSG)DevOps(4) : Ansible(2) - (MOSG)
DevOps(4) : Ansible(2) - (MOSG)
 
The GO Language : From Beginners to Gophers
The GO Language : From Beginners to GophersThe GO Language : From Beginners to Gophers
The GO Language : From Beginners to Gophers
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
 
Love Twig
Love TwigLove Twig
Love Twig
 
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
 
Implementation of 'go-like' language constructions in scala [english version]
Implementation of 'go-like' language constructions in scala [english version] Implementation of 'go-like' language constructions in scala [english version]
Implementation of 'go-like' language constructions in scala [english version]
 

Recently uploaded

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 

Recently uploaded (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
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
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Twig Templating