Twig is a template language for PHP that aims to be fast, flexible, and secure. It addresses some limitations of using PHP as a templating language by providing a more concise, template-oriented syntax with features like inheritance, macros, and filters. Templates are compiled to optimized PHP code for faster performance. Twig also includes security features like sandboxing to restrict what template code can do. The language provides common programming constructs like conditionals, loops, and expressions to build dynamic templates.
2. Cesare D'Amico
freelance php dev
Hi, nice to meet you!
cesare@wyrd.it
Twitter: __ce (double underscore)
http://cesaredami.co/
3. A bit of history
“So, you think PHP is a templating engine?”
Fabien Potencier - October 07, 2009
http://j.mp/lOnJU
In his opinion, php lacks:
●
concision (<?php echo $var ?>)
●
template-oriented syntax (foreach())
●
reusability
●
security + sandbox mode
4. A bit of history
Available solutions (October 2009):
●
Smarty3: slow
●
PHPTAL: HTML only
●
ez Components: slowest, no
inheritance
●
Dwoo: no inheritance
●
Calypso: broken (in his author's
words)
5. TWIG
●
Fast: Twig compiles templates to plain
optimized PHP code
●
Secure: sandbox mode for untrusted
template code
●
Flexible: powered by a flexible lexer
and parser, this allows to define
custom tags and filters
7. Control structures – for
{% for user in users %}
{% for i in 0..10 %}
{% for l in 'a'..'z' %}
{% for l in 'a'|upper..'z'|upper %}
{% for i in 0|range(10, 2) %}
8. Control structures – for
loop.index
loop.index0
loop.revindex *
loop.revindex0 *
loop.first (boolean)
loop.last * (boolean)
loop.length *
loop.parent
* Only for arrays and objects implementing
the Countable interface
9. Control structures – for
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% else %}
<li><em>no user found</em></li>
{% endfor %}
</ul>
10. Control structures – for
<ul>
{% for id in users|keys %}
<li>{{ id }}</li>
{% endfor %}
</ul>
11. Control structures – for
<ul>
{% for id, user in users %}
<li>{{ id }}: {{ user.username|e }}</li>
{% endfor %}
</ul>
12. Control structures – if
{% if users %}
<ul>
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
{% endif %}
13. Control structures – if/else
{% if kenny.sick %}
Kenny is sick.
{% elseif kenny.dead %}
You killed Kenny! You bastard!!!
{% else %}
Kenny looks okay so far
{% endif %}
29. import – hang yourself...
{% macro input(name, value, type, size) %}
<input type="{{ type|default('text') }}"
name="{{ name }}" value="{{ value|e }}"
size="{{ size|default(20) }}" />
{% endmacro %}
{% import _self as forms %}
<p>{{ forms.input('text','username') }}</p>
30. i18n – needs gettext!
{% trans "Hello World!" %}
{% trans string_var %}
{% trans %}Hello World!{% endtrans %}
{% trans %}Hello {{ name }}!{% endtrans %}
{% trans Hello {{ name }}! %}
35. Built-in extensions
Core (automatically registered)
Escaper
Sandbox
I18n
You can create your own extensions if you need so:
- implement interface Twig_ExtensionInterface
- extend Twig_Extension so you need only implement
needed methods
$twig>addExtension(
new Twig_Extension_Escaper()
);