Being Dangerous with Twig
Upcoming SlideShare
Loading in...5
×
 

Being Dangerous with Twig

on

  • 19,796 views

A presentation about Twig - the friendly templating engine written in PHP - given in San Francisco for Symfony Live 2011.

A presentation about Twig - the friendly templating engine written in PHP - given in San Francisco for Symfony Live 2011.

Statistics

Views

Total Views
19,796
Views on SlideShare
16,391
Embed Views
3,405

Actions

Likes
25
Downloads
145
Comments
1

84 Embeds 3,405

http://devtime.blogspot.com 1750
http://www.symfony-project.org 297
http://symfony2tips.blogspot.com 284
http://swik.net 224
http://www.symfony.es 134
http://symfony.com 101
http://gpupo.com 85
http://devtime.blogspot.fr 61
http://devtime.blogspot.ru 42
http://test.ical.ly 36
http://devtime.blogspot.de 35
http://devtime.blogspot.in 22
http://devtime.blogspot.it 21
http://devtime.blogspot.com.br 20
http://devtime.blogspot.co.uk 20
http://devtime.blogspot.ca 15
http://devtime.blogspot.com.ar 15
http://devtime.blogspot.com.es 14
http://static.slidesharecdn.com 14
http://rimzy.net 13
http://coderwall.com 10
http://symfony2tips.blogspot.de 10
http://symfony2tips.blogspot.fr 9
http://webcache.googleusercontent.com 8
http://devtime.blogspot.nl 8
http://devtime.blogspot.jp 7
http://www.testically.org 7
http://devtime.blogspot.hu 6
http://devtime.blogspot.cz 6
http://symfony2tips.blogspot.co.uk 6
http://feeds.feedburner.com 6
http://www.linkedin.com 6
http://translate.googleusercontent.com 6
http://symfony2tips.blogspot.nl 6
http://devtime.blogspot.se 5
http://devtime.blogspot.pt 5
http://symfony2tips.blogspot.com.es 5
http://devtime.blogspot.com.au 5
http://devtime.blogspot.co.il 5
http://xss.yandex.net 4
http://symfony2tips.blogspot.pt 4
http://devtime.blogspot.mx 4
http://devtime.blogspot.be 4
https://www.linkedin.com 4
http://www.techgig.com 3
http://devtime.blogspot.co.at 3
http://symfony2tips.blogspot.com.br 3
http://symfony2tips.blogspot.be 2
http://symfony2tips.blogspot.dk 2
http://symfony2tips.blogspot.jp 2
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Being Dangerous with Twig Being Dangerous with Twig Presentation Transcript

  • Symfony Live 2011being dangerous with Twiga short story by Ryan WeaverFebruary 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 dont clog up yourglobal PHP namespace, take advantage of true template inheritance, hang out withDjango programmers and be able to talk template syntax, enjoy true and non- invasiveoutput 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 hes 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 were hiring!
  • act 1 Why Twig?
  • because templateengines are awesome
  • Template enginesA template engine allows you to render apresentation (HTML, XML, etc) via a templatein a controlled environmentIt should allow special functionality thatmakes 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 templateswith 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 templatecompiles to an actual PHP object
  • seeing is believing
  • a moment oftemplating zen
  • “The template system is meant toexpress presentation, not program logic.” - Django documentation
  • Twig can easily be used anywhere
  • act 2 Twigs simple life
  • Twigs three tagsTwig 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 youre 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 youre *doing* something and not *printing* something, use this tag
  • Twigs three tags » do nothing: {# comment tag #} » say something: {{ print tag }} » do something: {% block tag %} its 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 – its 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 tagprint tag
  • Lets clean things up
  • filter to title-case the widget namefilters to strip tagsand shorten thewidgets description
  • your presenter is lying to you... » the “truncate” filter isnt 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 – well prove it later * https://github.com/fabpot/Twig-extensions
  • odd/even classes like a boss
  • Twig function cycles throughthe 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 filterchain filters to turn a list of tagsinto a comma-separated list
  • convenience, readability
  • even managementknows 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 uswhen were in the last iteration
  • the audacity: your speaker just lied again » but.... the “radius” function doesnt actually exist in Twig. but since its pretty handy, lets 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)* dont forget to import this file from your applications 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 Twigs syntax scares your clients... change it!
  • cool, what about debugging?a debug tag ships with the twig-extensions
  • the “debug” extension is available for youin Symfony2 - just enable it
  • prints every variable availableprints the foo variable
  • but weve only justscratched the surface
  • theres 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, hes a people-person » Twigs 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 – id love to hear from you!comments, feedback, questions http://joind.in/talk/view/2601