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
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!
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)
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
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
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
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”
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!
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)
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
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!
65.
66. cool, what about
debugging?
a debug tag ships with
the twig-extensions
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