a Sensio Labs Product

The flexible, fast, and secure
template language for PHP

Twig 0.9.7 released

Just a month after the release of Twig 0.9.6, I’m pleased to announce the availability of Twig 0.9.7.

As always, the documentation has been updated and the changelog lists all the changes we made. This post only mentions the main changes.

Backwards Incompatibilities

set tag

The set tag syntax changed for better readability:

{# before 0.9.7 #}

{% set title as "Title" %}

{# as of 0.9.7 #}

{% set title = "Title" %}

include tag

The sandboxed attribute of the include tag has been removed in favor of the new sandbox tag:

{# before 0.9.7 #}

{% include "foo.twig" sandboxed %}

{# as of 0.9.7 #}

{% sandbox %}
    {% include "foo.twig" %}
{% endsandbox %}

The include tag also accepts Twig_Template instances as argument.

Nodes

The Node sub-system has been refactored. If you have custom Nodes, you must update them and use the new API:

  • The Twig_NodeList class has been removed. If you tag extends it, you can safely replace it with Twig_Node and remove the getNodes() and setNodes() methods.

  • The __toString() method is now generated automatically by the Twig_Node base class. Most of the time, you can just remove it if you have defined it.

  • The main change is the constructor (read the Twig_Node PHPDoc for more information).

New Features

Dynamic and Conditional Inheritance

Such a big enhancement deserves it’s own blog post.

sandbox tag

As already mentioned, the new sandbox tag allows you to sandbox snippets of code:

{% sandbox %}
    {% include "foo.twig" %}
    {% include "bar.twig" %}
{% endsandbox %}

strict_variables setting

By default, Twig silently ignores undefined variables. While this is a good behavior for production environments, it makes debugging harder for developers. The strict_variables setting controls this behavior and is set to false by default. If enabled, Twig throws an exception when a variable is not defined.

Creating new Tags easily

When a developer want to create a new tag, he needs to understand many words: tokens, token streams, parsers, and compilers. Of course, he can just copy and paste existing code, but that’s far from perfect. As of Twig 0.9.7, there is a new high-level API to ease the creation of new tags. This is experimental and as the API can change, this is not yet documented.

Feel free to play with the new API and give us as much feedback as possible. To help you getting started faster, have a look at the code of the Symfony2 TwigBundle, which makes heavy use of the new API. You can start by looking at the TokenParser class and the Helpers classes.

Upgrading

Before upgrading, be sure to have read this post carefully as this release might break your existing code. Then, follow the standard steps:

  • Install the new version (via PEAR, SVN, Git, or by downloading the new package);

  • Remove all cached templates;

  • Test.

This website is powered by PHP and Twig.