Video: http://youtu.be/Jikkiqt-nBo
Twig! Yep, it's that fancy magic that's supposed to make theming in Drupal 8 as much fun as eating beef brisket at Rudy's Country Store in Austin (apologies to my veggie friends!). And in fact, Twig was *born* for this: a language that was created with one job in mind: making writing templates awesome and powerful. Oh, and to make you love using it.
In this talk, we'll learn about Twig from the ground-up: syntax, filters, inheritance and other tricks you can learn now to be ready for Drupal 8. We'll also look at how Twig looks inside Drupal, and how it compares to what you're used to in Drupal 7.
By the end, you'll know everything to start getting your Drupal 8 theme on and be shouting its praises from the hills! Ok, maybe not that last part (but I do love how excited Drupalers get), but you'll definitely have a new friend in your world: Twig.
2. A 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.
Being dangerous with Twig
12. 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)
@weaverryan
15. 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
@weaverryan
16. @weaverryan
we want the brevity
of templates
!
with the isolation of
object-oriented
programming
17. @weaverryan
so give me some Twiggy pudding
Twig is:
» fast
» flexible
» concise
» secure
» fully-featured
» Extensible
» designer-friendly
Twig offers:
» true inheritance
» real output escaping
» tons of filters
» custom tags
» great documentation
» global variables
» the “for-else” control
29. a. do nothing (comment tags)
{# comment #}
» totally ignored when rendered
@weaverryan
30. b. say something (print tags)
{{ 'print me!' }}
» simply prints the given expression
» equivalent to <?php echo
» If you're ultimately printing something,
use this tag
@weaverryan
31. @weaverryan
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
32. @weaverryan
Twig’s three tags
!
» do nothing: {# comment tag #}
» say something {{ ‘print tag’ }}
» do something {% block tag %}
It’s just that simple!
34. @weaverryan
expressions Twig guts
» like PHP, most everything inside a tag
is an expression
!
!
!
!
!
» expressions are the most interesting and
flexible part of Twig
44. @weaverryan
» a template that displays a list of “widgets”
in odd-even rows
» render info about each widget
» create basic, clean pagination
the test…
48. @weaverryan
» the “truncate” filter isn't part of Twig, but is
available via a library 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
your presenter is lying to you…
57. @weaverryan
» but.... the “radius” function doesn't
actually exist in Twig.
the audacity: your speaker just lied again
But since it's pretty handy, let's create it!
75. @weaverryan
/**
* settings.php
*
* Twig debugging:
*
* When debugging is enabled:
* - The markup is surrounded by HTML comments
* - The dump() function can be used
* - Templates are automatically recompiled
*/
$settings['twig_debug'] = TRUE;
89. @weaverryan
Am I working with an array?
<?php print render($page['header']); ?>
<div id="node-<?php print $node->nid; ?>">
or an object?
90. @weaverryan
Am I working with an array?
who cares!?
<article id="node-{{ node.id }}">
{{ page.header }}
91. @weaverryan
{{ page.header }}
» The dot notation is smart!
!
A. Is this an object with a public property?
B. Is this an array that has this key?
C. Is there a getHeader() function I can call?