SlideShare a Scribd company logo
Symfony Live 2011

being dangerous with Twig
a short story by Ryan Weaver

February 9, 2011
being dangerous with Twig

     A 5-step guide to using Twig – the fast, secure and
           extensible PHP templating engine – to create clean
   template code, leverage powerful filters, make your designers
  write you love letters, write template functions that don't clog up your
global PHP namespace, take advantage of true template inheritance, hang out with
Django programmers and be able to talk template syntax, enjoy true and non- invasive
output escaping, have more time for your family, control whitespace, add global
    variables to all templates, stop lying when you try to tell yourself that <?php echo looks better than a
 simple {{, use the fancy for-else control, Rock some macros – little reusable code functions, do awesome stuff like “{% if i is divisibleby 2 %}”,
   mediate in the simplicity of your templates and drink more green tea, sandbox your template and whitelist capabilities – allowing Twig to be used in a CMS,
        take advantage of the fact that all templates compile to PHP classes that can extend a base class of your choosing, impress your friends by changing the print tag from
            {{ var }} to [all-your-base] var [are-belong-to-us], confuse the guy next to you by changing “is” and “is not” to mean the opposite things and convince him that he's misunderstood
                                             how logical expressions are used in programming languages all along, create a custom tag that takes the body of its block and tweets it,
                                                                                       write templates the expresses presentation and not program logic.




                                                                    Ryan Weaver
                                                                  Symfony Live 2011
@weaverryan


    » symfony

    » documentation

    » collaboration

    » the lovely @leannapelham
iostudio: flying the symfony flag

  ●
      Advertising & Integrated Marketing Solutions

  • coming to you from
      » Nashville, TN
      » Washington, D.C.

  • 150 employees

  • and we're hiring!
act 1


        Why Twig?
because template
engines are awesome
Being Dangerous with Twig
Template engines

A template engine allows you to render a
presentation (HTML, XML, etc) via a template
in a controlled environment

It should allow special functionality that
makes creating templates easier (helpers,
template inheritance, etc)
a template engine
     is a tool
why not just render
 PHP templates?
PHP templating woes

» rendering template files is a hack: an include
  statement with output-buffering control

» no or faked template inheritance

» no isolation: PHP templates suck in any global
  variables or functions available
we need the brevity
   of templates

with the isolation of
  object-oriented
   programming
so give me some Twiggy pudding

 Twig is:                  Twig offers:
    » fast                    » true inheritance
    » flexible                » real output escaping
    » concise                 » tons of filters
    » secure                  » custom tags
    » fully-featured          » great documentation
    » Extensible              » global variables
    » designer-friendly       » the “for-else” control

             http://www.twig-project.org
Twig is concise

 and each template
compiles to an actual
     PHP object
seeing is believing
Being Dangerous with Twig
Being Dangerous with Twig
Being Dangerous with Twig
a moment of
templating zen
“The template system is meant to
express presentation, not program
             logic.”


             - Django documentation
Twig can easily be
 used anywhere
Being Dangerous with Twig
act 2


        Twig's simple life
Twig's three tags

Twig parses just three simple tags:

   » comment tag

   » print tag

   » block tag
a. do nothing (comment tags)

{# comment #}
   » totally ignored when rendered
b. say something (print tags)

{{ 'print me!' }}
   » prints the given expression
   » think “<?php echo”




   » If you're ultimately printing something, use
     this tag
c. do something (block tags)

{% set foo = 'inside a block tag' %}
  » used mostly for control-flow statements like if, for,
    include and block
  » can have beginning and end tags




   » if you're *doing* something and not *printing*
     something, use this tag
Twig's three tags


  » do nothing: {# comment tag #}

  » say something: {{ print tag }}

  » do something: {% block tag %}



   it's just that simple
act 3


        Everything is an
          expression
expressions: Twig guts

  » like PHP, most everything inside a tag
    is an expression




  » expressions are the most interesting and
    flexible part of Twig
Expressions   Block names
              Block-specific tokens
an expression can
 consist of many
 different things
strings, variables, arrays,
 functions, filters, tests,
       subscripts...
strings, numbers and variables
  » like any language, strings, numbers and
    variables are commonplace
arrays and hashes
  » arrays use [], hashes use {}
operators
  » twig has operators just like PHP, but extensible
    and with some extras
filters

   » a filter always follows a pipe (|) and modifies the
     value that precedes it
   » a filter may or may not take arguments
functions
  » just like PHP, returns a value based on some input
twig expresses himself


  » strings, numbers and variables

  » arrays and hashes

  » operators

  » filters

  » functions

   hey – it's simple like PHP, but flexible...
act 4


  twig on the battlefield
the test...


 » a template that displays a list of “widgets” in
   odd-even rows

 » render tags and other info about each widget

 » create basic, clean pagination
block tag




print tag
Let's clean things up
filter to title-case
   the widget name




filters to strip tags
and shorten the
widget's description
your presenter is lying to you...

   » the “truncate” filter isn't part of Twig, but
     is available via a repository of extensions

   » Everything in Twig is loaded via an Extension
     (even the core stuff)

   » Extensions are easy to use and create – we'll
     prove it later

   * https://github.com/fabpot/Twig-extensions
odd/even classes
   like a boss
Twig function cycles through
the given array items




               Special variable available inside
               all “for” loops.
               The “loop” variable knows other
               tricks like “loop.last” and
               “loop.revindex”
flex some filters
apply the date filter




chain filters to turn a list of tags
into a comma-separated list
convenience,
 readability
even management
knows what this does
pagination?
function returns a positive
      radius of numbers around the
      center (e.g. 3, 4, 5, 6, 7)




the awesome loop variable tells us
when we're in the last iteration
the audacity: your speaker just lied again

 » but.... the “radius” function doesn't actually
   exist in Twig.




  but since it's pretty handy, let's create it!
act 5


        Twig extensions
Twig extensions
 everything in Twig is loaded by an “Extension” class:

   » filters
   » functions
   » operators
   » tests (e.g. divisbleby)
   » custom tags

  Extensions are EASY!
step 1: create a class that extends Twig_Extension
step 2: tell Twig about the extension
step 2: tell Twig about the extension (Symfony2)




* don't forget to import this file from your
  application's configuration (i.e. app/config/config.yml)
step 3: add some guts to the extension class
step 4: Celebrate!!!
* buy a round of drinks
* watch the sun set
* kiss that cute girl at the
  coffee shop
I want more!
 ok great – do some reading!

   » http://www.twig-project.org/doc/advanced.html
   » http://www.twig-project.org/doc/extensions.html


 seriously – the Twig docs are quite excellent
act 6


        after-dinner mint

             mmm
screw with the Twig syntax

   » because Twig is totally sandboxed (i.e. you
    control exactly what can and cannot be done
    inside a template, Twig is a perfect fit for a CMS.


   » and if Twig's syntax scares your clients... change it!
Being Dangerous with Twig
cool, what about
     debugging?

a debug tag ships with
 the twig-extensions
the “debug” extension is available for you
in Symfony2 - just enable it
prints every variable
              available




prints the foo variable
but we've only just
scratched the surface
there's much much more

   » inheritance                   name can be:
                                    * an item on an array
                                    * property on an object
   » macros (reusable code bits)    * getName()


   » subscripts



  or you can force it to
  *just* fetch “name”
  as an array item
Twig, he's a people-person

   » Twig's loves contributions, so *get involved*!


   » https://github.com/fabpot/twig
   » https://github.com/fabpot/twig-extensions
   » http://groups.google.com/group/twig-users
thank you


                        Ryan Weaver
                        iostudio
                        @weaverryan
                        www.thatsquality.com
                        ryan [at] thatsquality.com


           reach out to me – i'd love to hear from you!

comments, feedback, questions


                  http://joind.in/talk/view/2601

More Related Content

What's hot (19)

PDF
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
Matthias Noback
 
PDF
Learning puppet chapter 2
Vishal Biyani
 
PDF
Twig for Drupal 8 and PHP | Presented at OC Drupal
webbywe
 
PDF
Symfony Components 2.0 on PHP 5.3
Fabien Potencier
 
PDF
Puppet for Sys Admins
Puppet
 
PDF
Learning Puppet Chapter 1
Vishal Biyani
 
PDF
PHP 5.3 in practice
Fabien Potencier
 
PDF
Getting big without getting fat, in perl
Dean Hamstead
 
PDF
Symfony Components
guest0de7c2
 
PDF
Php go vrooom!
Elizabeth Smith
 
PDF
The Naked Bundle - Symfony Usergroup Belgium
Matthias Noback
 
PDF
Symfony 4 Workshop - Limenius
Ignacio Martín
 
PDF
Puppet at Pinterest
Puppet
 
PDF
How Symfony Changed My Life
Matthias Noback
 
PDF
The Naked Bundle - Symfony Live London 2014
Matthias Noback
 
ODP
Object Oriented Design Patterns for PHP
RobertGonzalez
 
PDF
Puppet at GitHub / ChatOps
Puppet
 
PPT
Php mysql ppt
Karmatechnologies Pvt. Ltd.
 
PDF
Design pattern in Symfony2 - Nanos gigantium humeris insidentes
Giulio De Donato
 
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
Matthias Noback
 
Learning puppet chapter 2
Vishal Biyani
 
Twig for Drupal 8 and PHP | Presented at OC Drupal
webbywe
 
Symfony Components 2.0 on PHP 5.3
Fabien Potencier
 
Puppet for Sys Admins
Puppet
 
Learning Puppet Chapter 1
Vishal Biyani
 
PHP 5.3 in practice
Fabien Potencier
 
Getting big without getting fat, in perl
Dean Hamstead
 
Symfony Components
guest0de7c2
 
Php go vrooom!
Elizabeth Smith
 
The Naked Bundle - Symfony Usergroup Belgium
Matthias Noback
 
Symfony 4 Workshop - Limenius
Ignacio Martín
 
Puppet at Pinterest
Puppet
 
How Symfony Changed My Life
Matthias Noback
 
The Naked Bundle - Symfony Live London 2014
Matthias Noback
 
Object Oriented Design Patterns for PHP
RobertGonzalez
 
Puppet at GitHub / ChatOps
Puppet
 
Design pattern in Symfony2 - Nanos gigantium humeris insidentes
Giulio De Donato
 

Similar to Being Dangerous with Twig (20)

PDF
Twig, the flexible, fast, and secure template language for PHP
Fabien Potencier
 
PPT
Powerful and flexible templates with Twig
Michael Peacock
 
KEY
Twig for Drupal @ Frontendunited Amsterdam 2012
Rene Bakx
 
PDF
Twig Brief, Tips&Tricks
Andrei Burian
 
PDF
Making Sense of Twig
Brandon Kelly
 
PPTX
Use Symfony2 components inside WordPress
Maurizio Pelizzone
 
PDF
Twig Templating
Rj Bautista
 
PDF
Twig tips and tricks
Javier Eguiluz
 
PDF
Twig
Sir-Arturio
 
PDF
Introduction to Twig
markstory
 
PDF
Twig & D8 - DrupalCamp Baltics 2013 - Tallinn
Sir-Arturio
 
PPTX
Twig internals - Maksym MoskvychevTwig internals maksym moskvychev
DrupalCampDN
 
PDF
Love Twig
Antonio Peric-Mazar
 
PDF
Empezando con Twig
Ismael Ambrosi
 
PDF
Twig in drupal8
Wizzlern
 
PDF
Mastering Twig (DrupalCon Barcelona 2015)
Javier Eguiluz
 
PPTX
One-hour Drupal 8 Theming
Mediacurrent
 
PDF
Drupal 8 templating with twig
Taras Omelianenko
 
PDF
Terrific Composer Workshop
Remo Brunschwiler
 
PDF
Drupal 8 - Core and API Changes
Shabir Ahmad
 
Twig, the flexible, fast, and secure template language for PHP
Fabien Potencier
 
Powerful and flexible templates with Twig
Michael Peacock
 
Twig for Drupal @ Frontendunited Amsterdam 2012
Rene Bakx
 
Twig Brief, Tips&Tricks
Andrei Burian
 
Making Sense of Twig
Brandon Kelly
 
Use Symfony2 components inside WordPress
Maurizio Pelizzone
 
Twig Templating
Rj Bautista
 
Twig tips and tricks
Javier Eguiluz
 
Introduction to Twig
markstory
 
Twig & D8 - DrupalCamp Baltics 2013 - Tallinn
Sir-Arturio
 
Twig internals - Maksym MoskvychevTwig internals maksym moskvychev
DrupalCampDN
 
Empezando con Twig
Ismael Ambrosi
 
Twig in drupal8
Wizzlern
 
Mastering Twig (DrupalCon Barcelona 2015)
Javier Eguiluz
 
One-hour Drupal 8 Theming
Mediacurrent
 
Drupal 8 templating with twig
Taras Omelianenko
 
Terrific Composer Workshop
Remo Brunschwiler
 
Drupal 8 - Core and API Changes
Shabir Ahmad
 
Ad

More from Ryan Weaver (15)

PDF
Webpack Encore Symfony Live 2017 San Francisco
Ryan Weaver
 
PDF
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
Ryan Weaver
 
PDF
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Ryan Weaver
 
PDF
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Ryan Weaver
 
PDF
Symfony: Your Next Microframework (SymfonyCon 2015)
Ryan Weaver
 
PDF
Guard Authentication: Powerful, Beautiful Security
Ryan Weaver
 
PDF
Grand Rapids PHP Meetup: Behavioral Driven Development with Behat
Ryan Weaver
 
PDF
Master the New Core of Drupal 8 Now: with Symfony and Silex
Ryan Weaver
 
PDF
Silex: Microframework y camino fácil de aprender Symfony
Ryan Weaver
 
PDF
Drupal 8: Huge wins, a Bigger Community, and why you (and I) will Love it
Ryan Weaver
 
PDF
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
Ryan Weaver
 
PDF
The Wonderful World of Symfony Components
Ryan Weaver
 
PDF
A PHP Christmas Miracle - 3 Frameworks, 1 app
Ryan Weaver
 
PDF
Doctrine2 In 10 Minutes
Ryan Weaver
 
PDF
The Art of Doctrine Migrations
Ryan Weaver
 
Webpack Encore Symfony Live 2017 San Francisco
Ryan Weaver
 
The Coolest Symfony Components you’ve never heard of - DrupalCon 2017
Ryan Weaver
 
Finally, Professional Frontend Dev with ReactJS, WebPack & Symfony (Symfony C...
Ryan Weaver
 
Symfony Guard Authentication: Fun with API Token, Social Login, JWT and more
Ryan Weaver
 
Symfony: Your Next Microframework (SymfonyCon 2015)
Ryan Weaver
 
Guard Authentication: Powerful, Beautiful Security
Ryan Weaver
 
Grand Rapids PHP Meetup: Behavioral Driven Development with Behat
Ryan Weaver
 
Master the New Core of Drupal 8 Now: with Symfony and Silex
Ryan Weaver
 
Silex: Microframework y camino fácil de aprender Symfony
Ryan Weaver
 
Drupal 8: Huge wins, a Bigger Community, and why you (and I) will Love it
Ryan Weaver
 
Cool like a Frontend Developer: Grunt, RequireJS, Bower and other Tools
Ryan Weaver
 
The Wonderful World of Symfony Components
Ryan Weaver
 
A PHP Christmas Miracle - 3 Frameworks, 1 app
Ryan Weaver
 
Doctrine2 In 10 Minutes
Ryan Weaver
 
The Art of Doctrine Migrations
Ryan Weaver
 
Ad

Recently uploaded (20)

PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
July Patch Tuesday
Ivanti
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
July Patch Tuesday
Ivanti
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 

Being Dangerous with Twig

  • 1. Symfony Live 2011 being dangerous with Twig a short story by Ryan Weaver February 9, 2011
  • 2. being dangerous with Twig A 5-step guide to using Twig – the fast, secure and extensible PHP templating engine – to create clean template code, leverage powerful filters, make your designers write you love letters, write template functions that don't clog up your global PHP namespace, take advantage of true template inheritance, hang out with Django programmers and be able to talk template syntax, enjoy true and non- invasive output escaping, have more time for your family, control whitespace, add global variables to all templates, stop lying when you try to tell yourself that <?php echo looks better than a simple {{, use the fancy for-else control, Rock some macros – little reusable code functions, do awesome stuff like “{% if i is divisibleby 2 %}”, mediate in the simplicity of your templates and drink more green tea, sandbox your template and whitelist capabilities – allowing Twig to be used in a CMS, take advantage of the fact that all templates compile to PHP classes that can extend a base class of your choosing, impress your friends by changing the print tag from {{ var }} to [all-your-base] var [are-belong-to-us], confuse the guy next to you by changing “is” and “is not” to mean the opposite things and convince him that he's misunderstood how logical expressions are used in programming languages all along, create a custom tag that takes the body of its block and tweets it, write templates the expresses presentation and not program logic. Ryan Weaver Symfony Live 2011
  • 3. @weaverryan » symfony » documentation » collaboration » the lovely @leannapelham
  • 4. iostudio: flying the symfony flag ● Advertising & Integrated Marketing Solutions • coming to you from » Nashville, TN » Washington, D.C. • 150 employees • and we're hiring!
  • 5. act 1 Why Twig?
  • 8. Template engines A template engine allows you to render a presentation (HTML, XML, etc) via a template in a controlled environment It should allow special functionality that makes creating templates easier (helpers, template inheritance, etc)
  • 9. a template engine is a tool
  • 10. why not just render PHP templates?
  • 11. PHP templating woes » rendering template files is a hack: an include statement with output-buffering control » no or faked template inheritance » no isolation: PHP templates suck in any global variables or functions available
  • 12. we need the brevity of templates with the isolation of object-oriented programming
  • 13. so give me some Twiggy pudding Twig is: Twig offers: » fast » true inheritance » flexible » real output escaping » concise » tons of filters » secure » custom tags » fully-featured » great documentation » Extensible » global variables » designer-friendly » the “for-else” control http://www.twig-project.org
  • 14. Twig is concise and each template compiles to an actual PHP object
  • 20. “The template system is meant to express presentation, not program logic.” - Django documentation
  • 21. Twig can easily be used anywhere
  • 23. act 2 Twig's simple life
  • 24. Twig's three tags Twig parses just three simple tags: » comment tag » print tag » block tag
  • 25. a. do nothing (comment tags) {# comment #} » totally ignored when rendered
  • 26. b. say something (print tags) {{ 'print me!' }} » prints the given expression » think “<?php echo” » If you're ultimately printing something, use this tag
  • 27. c. do something (block tags) {% set foo = 'inside a block tag' %} » used mostly for control-flow statements like if, for, include and block » can have beginning and end tags » if you're *doing* something and not *printing* something, use this tag
  • 28. Twig's three tags » do nothing: {# comment tag #} » say something: {{ print tag }} » do something: {% block tag %} it's just that simple
  • 29. act 3 Everything is an expression
  • 30. expressions: Twig guts » like PHP, most everything inside a tag is an expression » expressions are the most interesting and flexible part of Twig
  • 31. Expressions Block names Block-specific tokens
  • 32. an expression can consist of many different things
  • 33. strings, variables, arrays, functions, filters, tests, subscripts...
  • 34. strings, numbers and variables » like any language, strings, numbers and variables are commonplace
  • 35. arrays and hashes » arrays use [], hashes use {}
  • 36. operators » twig has operators just like PHP, but extensible and with some extras
  • 37. filters » a filter always follows a pipe (|) and modifies the value that precedes it » a filter may or may not take arguments
  • 38. functions » just like PHP, returns a value based on some input
  • 39. twig expresses himself » strings, numbers and variables » arrays and hashes » operators » filters » functions hey – it's simple like PHP, but flexible...
  • 40. act 4 twig on the battlefield
  • 41. the test... » a template that displays a list of “widgets” in odd-even rows » render tags and other info about each widget » create basic, clean pagination
  • 44. filter to title-case the widget name filters to strip tags and shorten the widget's description
  • 45. your presenter is lying to you... » the “truncate” filter isn't part of Twig, but is available via a repository of extensions » Everything in Twig is loaded via an Extension (even the core stuff) » Extensions are easy to use and create – we'll prove it later * https://github.com/fabpot/Twig-extensions
  • 46. odd/even classes like a boss
  • 47. Twig function cycles through the given array items Special variable available inside all “for” loops. The “loop” variable knows other tricks like “loop.last” and “loop.revindex”
  • 49. apply the date filter chain filters to turn a list of tags into a comma-separated list
  • 53. function returns a positive radius of numbers around the center (e.g. 3, 4, 5, 6, 7) the awesome loop variable tells us when we're in the last iteration
  • 54. the audacity: your speaker just lied again » but.... the “radius” function doesn't actually exist in Twig. but since it's pretty handy, let's create it!
  • 55. act 5 Twig extensions
  • 56. Twig extensions everything in Twig is loaded by an “Extension” class: » filters » functions » operators » tests (e.g. divisbleby) » custom tags Extensions are EASY!
  • 57. step 1: create a class that extends Twig_Extension
  • 58. step 2: tell Twig about the extension
  • 59. step 2: tell Twig about the extension (Symfony2) * don't forget to import this file from your application's configuration (i.e. app/config/config.yml)
  • 60. step 3: add some guts to the extension class
  • 61. step 4: Celebrate!!! * buy a round of drinks * watch the sun set * kiss that cute girl at the coffee shop
  • 62. I want more! ok great – do some reading! » http://www.twig-project.org/doc/advanced.html » http://www.twig-project.org/doc/extensions.html seriously – the Twig docs are quite excellent
  • 63. act 6 after-dinner mint mmm
  • 64. screw with the Twig syntax » because Twig is totally sandboxed (i.e. you control exactly what can and cannot be done inside a template, Twig is a perfect fit for a CMS. » and if Twig's syntax scares your clients... change it!
  • 66. cool, what about debugging? a debug tag ships with the twig-extensions
  • 67. the “debug” extension is available for you in Symfony2 - just enable it
  • 68. prints every variable available prints the foo variable
  • 69. but we've only just scratched the surface
  • 70. there's much much more » inheritance name can be: * an item on an array * property on an object » macros (reusable code bits) * getName() » subscripts or you can force it to *just* fetch “name” as an array item
  • 71. Twig, he's a people-person » Twig's loves contributions, so *get involved*! » https://github.com/fabpot/twig » https://github.com/fabpot/twig-extensions » http://groups.google.com/group/twig-users
  • 72. thank you Ryan Weaver iostudio @weaverryan www.thatsquality.com ryan [at] thatsquality.com reach out to me – i'd love to hear from you! comments, feedback, questions http://joind.in/talk/view/2601