a Sensio Labs Product

The flexible, fast, and secure
template language for PHP

Twig 1.0.0-RC1 released

I’m really thrilled to announce the immediate availability of the first release candidate of Twig 1.0 which marks the beginning of the 1.0 release cycle. That means that from now on, backward compatibility will be preserved.

Besides many small optimizations and better error messages (see the changelog), this release has two major new features: functions and globals. They make extending Twig as easy as it can get.

Functions

Twig has many extension points, and each release adds more of them. The two most useful are filters and tags. Filters are easy to create but restricted to transform values; and tags are hard to create but not that useful as they should only define language constructs (so adding some is pretty rare).

So until now, as something was missing between filters and tags, tags were commonly used to add more complex features to your templates (and Twig core was no exception). This gap is now closed thanks to the introduction of functions.

A function is like a PHP function. It is also like a Twig macro, but defined in your PHP code. It allows the developer to add features that generate content. Let’s see two examples where a core function and a core tag have been converted to functions:

{# range() is not a filter as it does not really convert 1 to something else #}
{# 1 just happens to be the first argument of range #}
{% for in 1|range(10) %}

{# now, range is a proper function that generates an array #}
{% for i range(1, 10) %}

{# display is not a language construct but a way to display a block #}
{% display title %}

{# the block function is now the proper way to display a block #}
{{ block('title') }}

Functions can be used in any expression as well:

{{ range('a', 'z')|join(', ') }}

Here is the list of all filters and tags that have been converted to functions in Twig 1.0 RC1:

BeforeAfter
{% for in 1|range(10) %} {% for i range(1, 10) %}
{% if status is "Post::PUBLISHED"|constant %} {% if status is constant("Post::PUBLISHED") %}
{{ ['odd', 'even']|cycle(i) }} {{ cycle(['odd', 'even'], i) }}
{% parent %} {{ parent() }}
{% display title %} {{ block('title') }}

Globals

Sometimes, it is useful to register a global variable so that it is always available in your template:

// a global can be a constant
$twig->addGlobal('pi', 3.14);

// or any other valid PHP expression, like an object
$twig->addGlobal('request', new Request());

Global variables can then be used as any other variable in your template:

{{ pi }}

{{ request.params('name') }}

Twig Extension Points

Whenever you want to extend Twig, keep in mind the following:

What? Implementation difficulty? How often? When?
macro trivial frequent Content generation
global trivial frequent Helper object
function trivial frequent Content generation
filter trivial frequent Value transformation
tag complex rare DSL language construct
test trivial rare Boolean decision
operator trivial rare Values transformation
This website is powered by PHP and Twig.