being dangerous with twig
DESCRIPTION
A presentation about Twig - the friendly templating engine written in PHP - given in San Francisco for Symfony Live 2011.TRANSCRIPT
![Page 1: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/1.jpg)
being dangerous with Twiga short story by Ryan Weaver
February 9, 2011
Symfony Live 2011
![Page 2: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/2.jpg)
A 5-step guide to using Twig – the fast, secure andextensible PHP templating engine – to create clean
template code, leverage powerful filters, make your designerswrite 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 asimple {{, 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 misunderstoodhow 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 WeaverSymfony Live 2011
being dangerous with Twig
![Page 3: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/3.jpg)
@weaverryan
» symfony
» documentation
» collaboration
» the lovely @leannapelham
![Page 4: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/4.jpg)
● Advertising & Integrated Marketing Solutions
• coming to you from» Nashville, TN» Washington, D.C.
• 150 employees
• and we're hiring!
iostudio: flying the symfony flag
![Page 5: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/5.jpg)
Why Twig?
act 1
![Page 6: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/6.jpg)
because template engines are awesome
![Page 7: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/7.jpg)
![Page 8: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/8.jpg)
Template engines
A template engine allows you to render apresentation (HTML, XML, etc) via a templatein a controlled environment
It should allow special functionality thatmakes creating templates easier (helpers,template inheritance, etc)
![Page 9: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/9.jpg)
a template engineis a tool
![Page 10: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/10.jpg)
why not just renderPHP templates?
![Page 11: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/11.jpg)
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
![Page 12: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/12.jpg)
we need the brevity of templates
with the isolation of object-oriented programming
![Page 13: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/13.jpg)
so give me some Twiggy pudding
Twig is:» fast» flexible» concise» secure» fully-featured» Extensible» designer-friendly
http://www.twig-project.org
Twig offers:» true inheritance» real output escaping» tons of filters» custom tags» great documentation» global variables» the “for-else” control
![Page 14: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/14.jpg)
Twig is concise
and each template compiles to an actual
PHP object
![Page 15: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/15.jpg)
seeing is believing
![Page 16: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/16.jpg)
![Page 17: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/17.jpg)
![Page 18: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/18.jpg)
![Page 19: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/19.jpg)
a moment oftemplating zen
![Page 20: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/20.jpg)
“The template system is meant to express presentation, not program
logic.”
- Django documentation
![Page 21: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/21.jpg)
Twig can easily be used anywhere
![Page 22: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/22.jpg)
![Page 23: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/23.jpg)
act 2
Twig's simple life
![Page 24: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/24.jpg)
Twig's three tags
Twig parses just three simple tags:
» comment tag
» print tag
» block tag
![Page 25: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/25.jpg)
a. do nothing (comment tags)
{# comment #}» totally ignored when rendered
![Page 26: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/26.jpg)
b. say something (print tags)
{{ 'print me!' }}» prints the given expression» think “<?php echo”
» If you're ultimately printing something, use this tag
![Page 27: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/27.jpg)
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
![Page 28: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/28.jpg)
Twig's three tags
» do nothing: {# comment tag #}
» say something: {{ print tag }}
» do something: {% block tag %}
it's just that simple
![Page 29: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/29.jpg)
act 3
Everything is anexpression
![Page 30: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/30.jpg)
expressions: Twig guts
» like PHP, most everything inside a tag is an expression
» expressions are the most interesting and flexible part of Twig
![Page 31: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/31.jpg)
Expressions Block names
Block-specific tokens
![Page 32: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/32.jpg)
an expression can consist of many different things
![Page 33: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/33.jpg)
strings, variables, arrays, functions, filters, tests,
subscripts...
![Page 34: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/34.jpg)
strings, numbers and variables
» like any language, strings, numbers and variables are commonplace
![Page 35: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/35.jpg)
arrays and hashes
» arrays use [], hashes use {}
![Page 36: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/36.jpg)
operators
» twig has operators just like PHP, but extensible and with some extras
![Page 37: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/37.jpg)
filters
» a filter always follows a pipe (|) and modifies the value that precedes it
» a filter may or may not take arguments
![Page 38: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/38.jpg)
functions
» just like PHP, returns a value based on some input
![Page 39: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/39.jpg)
twig expresses himself
» strings, numbers and variables
» arrays and hashes
» operators
» filters
» functions
hey – it's simple like PHP, but flexible...
![Page 40: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/40.jpg)
act 4
twig on the battlefield
![Page 41: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/41.jpg)
the test...
» a template that displays a list of “widgets” inodd-even rows
» render tags and other info about each widget
» create basic, clean pagination
![Page 42: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/42.jpg)
block tag
print tag
![Page 43: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/43.jpg)
Let's clean things up
![Page 44: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/44.jpg)
filter to title-casethe widget name
filters to strip tagsand shorten thewidget's description
![Page 45: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/45.jpg)
your presenter is lying to you...
» the “truncate” filter isn't part of Twig, butis 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'llprove it later
* https://github.com/fabpot/Twig-extensions
![Page 46: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/46.jpg)
odd/even classeslike a boss
![Page 47: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/47.jpg)
Twig function cycles throughthe given array items
Special variable available insideall “for” loops.
The “loop” variable knows othertricks like “loop.last” and“loop.revindex”
![Page 48: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/48.jpg)
flex some filters
![Page 49: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/49.jpg)
apply the date filter
chain filters to turn a list of tagsinto a comma-separated list
![Page 50: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/50.jpg)
convenience,readability
![Page 51: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/51.jpg)
even managementknows what this does
![Page 52: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/52.jpg)
pagination?
![Page 53: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/53.jpg)
function returns a positiveradius of numbers around thecenter (e.g. 3, 4, 5, 6, 7)
the awesome loop variable tells uswhen we're in the last iteration
![Page 54: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/54.jpg)
the audacity: your speaker just lied again
» but.... the “radius” function doesn't actuallyexist in Twig.
but since it's pretty handy, let's create it!
![Page 55: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/55.jpg)
act 5
Twig extensions
![Page 56: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/56.jpg)
Twig extensions
everything in Twig is loaded by an “Extension” class:
» filters
» functions
» operators
» tests (e.g. divisbleby)
» custom tags
Extensions are EASY!
![Page 57: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/57.jpg)
step 1: create a class that extends Twig_Extension
![Page 58: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/58.jpg)
step 2: tell Twig about the extension
![Page 59: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/59.jpg)
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)
![Page 60: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/60.jpg)
step 3: add some guts to the extension class
![Page 61: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/61.jpg)
* buy a round of drinks
* watch the sun set
* kiss that cute girl at the coffee shop
step 4: Celebrate!!!
![Page 62: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/62.jpg)
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
![Page 63: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/63.jpg)
act 6
after-dinner mint
mmm
![Page 64: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/64.jpg)
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!
![Page 65: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/65.jpg)
![Page 66: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/66.jpg)
cool, what aboutdebugging?
a debug tag ships withthe twig-extensions
![Page 67: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/67.jpg)
the “debug” extension is available for youin Symfony2 - just enable it
![Page 68: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/68.jpg)
prints every variableavailable
prints the foo variable
![Page 69: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/69.jpg)
but we've only justscratched the surface
![Page 70: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/70.jpg)
there's much much more
» inheritance
» macros (reusable code bits)
» subscripts
name can be: * an item on an array * property on an object * getName()
or you can force it to*just* fetch “name”as an array item
![Page 71: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/71.jpg)
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
![Page 72: Being Dangerous with Twig](https://reader034.vdocuments.us/reader034/viewer/2022052303/554fbadcb4c9053d018b4724/html5/thumbnails/72.jpg)
Ryan [email protected] [at] thatsquality.com
thank you
reach out to me – i'd love to hear from you!
http://joind.in/talk/view/2601
comments, feedback, questions