Twig 1.2.0 RC1 released
I have just published the first Twig 1.2 release candidate. This version is fully compatible with previous versions of Twig (just don’t forget to clear the cache after upgrading).
In this post, I want to highlight some of the changes we have made to Twig to make it even better and more flexible than what it is today.
To learn more about all the changes, you can also have a look at the full CHANGELOG or browse the full code diff.
Getting dynamic attributes on variables
One of the best Twig feature is the way is abstracts access to the variable attributes: it allows the developers to mock a website with plain arrays and then switch to using objects later on without changing anything in the templates. That’s great.
But sometimes, you need to access a “dynamic” attribute on a variable. For instance, let’s say you want to display a data grid for a list of objects and the user gives you a list of columns to display. As of Twig 1.2, this is really easy to achieve:
{% for object in objects %}
{% for column in columns %}
{{ attribute(object, column) }}
{% endfor %}
{% endfor %}
The resolution algorithm for the attribute function is the same as the one
used for the . notation, except that the attribute can be any valid Twig
expression.
Condition support on for loops
Unlike in PHP, it’s not possible to break or continue in a loop. But you
can now filter the sequence during iteration which allows you to skip items.
The following example skips all the inactive users:
<ul>
{% for user in users if user.active %}
<li>{{ user.username }}</li>
{% endfor %}
</ul>
The advantage is that the special loop variable will count correctly thus
not counting the users not iterated over.
Internationalized Identifiers
Do you know that PHP identifiers (variable, function, method, and class names) can use characters outside of the standard ASCII character set? Actually, the following PHP code snippet is valid:
<?php
class ☃
{
public function ☃()
{
echo '☃';
}
}
As of 1.2, Twig accepts the same set of characters as PHP for block, tag, function, filter, and macro identifiers. So, the following Twig snippet is now valid:
{{ foo|☃() }}
{{ ☃() }}
Note that the allowed characters is just a subset of unicode (the actual
regexp is [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*); and of course, it
should be used with caution!
Better fallback mechanisms
You can now mark an include with ignore missing in which case Twig will
ignore the statement if the template to be ignored does not exist:
{% include "user_sidebar.html" ignore missing %}
You can also provide a list of templates that are checked for existence (that
also works for the extends tag):
{% include ['user_sidebar.html', 'sidebar.html'] %}
{% extends ['user_layout.html', 'layout.html'] %}
And of course, you can combine the two:
{% include ['user_sidebar.html', 'sidebar.html'] ignore missing %}
Internally, the resolution is done via a new
Twig_Environment::resolveTemplates() method that you can also use in your
PHP code.
Speaking of using fallback templates, Twig 1.2 comes with a new loader,
Twig_Loader_Chain, which allows to chain several loaders to find a template.
You are more than welcome to try out this version on your current projects; and be sure to report any regressions or bugs before we mark this release as stable.