loaders are available and you can also write your own if you want to load
templates from a database or other resources.
-.. caution::
-
- Before Twig 0.9.3, the ``cache`` option did not exist, and the cache
- directory was passed as a second argument of the loader.
-
.. note::
Notice that the second argument of the environment is an array of options.
the ``auto_reload`` option, it will be determined automatically based on the
``debug`` value.
-* ``strict_variables`` (new in Twig 0.9.7): If set to ``false``, Twig will
- silently ignore invalid variables (variables and or attributes/methods that
- do not exist) and replace them with a ``null`` value. When set to ``true``,
- Twig throws an exception instead (default to ``false``).
-
-* ``autoescape`` (new in Twig 0.9.10): If set to ``true``, auto-escaping will
- be enabled by default for all templates (default to ``true``).
+* ``strict_variables``: If set to ``false``, Twig will silently ignore invalid
+ variables (variables and or attributes/methods that do not exist) and
+ replace them with a ``null`` value. When set to ``true``, Twig throws an
+ exception instead (default to ``false``).
-* ``optimizations`` (new in Twig 0.9.10): A flag that indicates which
- optimizations to apply (default to ``-1`` -- all optimizations are enabled;
- set it to ``0`` to disable).
+* ``autoescape``: If set to ``true``, auto-escaping will be enabled by default
+ for all templates (default to ``true``).
-.. caution::
-
- Before Twig 0.9.3, the ``cache`` and ``auto_reload`` options did not
- exist. They were passed as a second and third arguments of the filesystem
- loader respectively.
+* ``optimizations``: A flag that indicates which optimizations to apply
+ (default to ``-1`` -- all optimizations are enabled; set it to ``0`` to
+ disable).
Loaders
-------
-.. caution::
-
- This section describes the loaders as implemented in Twig version 0.9.4
- and above.
-
Loaders are responsible for loading templates from a resource such as the file
system.
* *Twig_Extension_Sandbox*: Adds a sandbox mode to the default Twig
environment, making it safe to evaluated untrusted code.
-* *Twig_Extension_Optimizer*: Optimizers the node tree before compilation (as
- of Twig 0.9.10).
+* *Twig_Extension_Optimizer*: Optimizers the node tree before compilation.
The core, escaper, and optimizer extensions do not need to be added to the
Twig environment, as they are registered by default. You can disable an
The ``autoescape`` tag has no effect on included files.
-The escaping rules are implemented as follows (it describes the behavior of
-Twig 0.9.9 and above):
+The escaping rules are implemented as follows:
* Literals (integers, booleans, arrays, ...) used in the template directly as
variables or filter arguments are never automatically escaped:
$sandbox = new Twig_Extension_Sandbox($policy, true);
-Optimizer Extension (as of Twig 0.9.10)
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Optimizer Extension
+~~~~~~~~~~~~~~~~~~~
The ``optimizer`` extension optimizes the node tree before compilation::
* if not, and if ``foo`` is an object, check that ``bar`` is a valid method
(even if ``bar`` is the constructor - use ``__construct()`` instead);
* if not, and if ``foo`` is an object, check that ``getBar`` is a valid method;
- * if not, and if ``foo`` is an object, check that ``isBar`` is a valid method (as of Twig 0.9.9);
+ * if not, and if ``foo`` is an object, check that ``isBar`` is a valid method;
* if not, return a ``null`` value.
``foo['bar']`` on the other hand works mostly the same with the a small
Twig always references the following variables:
-* ``_self``: references the current template (was ``self`` before 0.9.9);
+* ``_self``: references the current template;
* ``_context``: references the current context;
-* ``_charset``: references the current charset (as of 0.9.9).
+* ``_charset``: references the current charset.
Filters
-------
{% block title page_title|title %}
-Dynamic Inheritance (as of Twig 0.9.7)
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Dynamic Inheritance
+~~~~~~~~~~~~~~~~~~~
Twig supports dynamic inheritance by using a variable as the base template:
$twig->display('template.twig', array('layout' => $layout));
-Conditional Inheritance (as of Twig 0.9.7)
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Conditional Inheritance
+~~~~~~~~~~~~~~~~~~~~~~~
As a matter of fact, the template name can be any valid expression. So, it's
also possible to make the inheritance mechanism conditional:
``Traversable`` interface.
If you do need to iterate over a sequence of numbers, you can use the ``..``
-operator (as of Twig 0.9.5):
+operator:
.. code-block:: jinja
The ``loop.length``, ``loop.revindex``, ``loop.revindex0``, and
``loop.last`` variables are only available for PHP arrays, or objects that
- implement the ``Countable`` interface (as of Twig 0.9.7).
+ implement the ``Countable`` interface.
.. note::
{% endfor %}
</ul>
-.. note::
-
- On Twig before 0.9.3, you need to use the ``items`` filter to access both
- the keys and values (``{% for key, value in users|items %}``).
-
If
~~
{% set foo, bar = 'foo', 'bar' %}
-The ``set`` tag can also be used to 'capture' chunks of HTML (new in Twig
-0.9.6):
+The ``set`` tag can also be used to 'capture' chunks of HTML:
.. code-block:: jinja
{# no variable will be accessible #}
{% include 'foo' only %}
-.. note::
-
- The ``with`` keyword is supported as of Twig 0.9.5. The ``only`` keyword
- is supported as of Twig 0.9.9.
-
.. tip::
When including a template created by an end user, you should consider
writing the number down. If a dot is present the number is a float,
otherwise an integer.
-* ``["foo", "bar"]`` (new in Twig 0.9.5): Arrays are defined by a sequence of
- expressions separated by a comma (``,``) and wrapped with squared brackets
- (``[]``).
+* ``["foo", "bar"]``: Arrays are defined by a sequence of expressions
+ separated by a comma (``,``) and wrapped with squared brackets (``[]``).
-* ``{"foo": "bar"}`` (new in Twig 0.9.10): Hashes are defined by a list of keys
- and values separated by a comma (``,``) and wrapped with curly braces (``{}``).
- A value can be any valid expression.
+* ``{"foo": "bar"}``: Hashes are defined by a list of keys and values
+ separated by a comma (``,``) and wrapped with curly braces (``{}``). A value
+ can be any valid expression.
* ``true`` / ``false``: ``true`` represents the true value, ``false``
represents the false value.
The following comparison operators are supported in any expression: ``==``,
``!=``, ``<``, ``>``, ``>=``, and ``<=``.
-Containment Operator (new in Twig 0.9.5)
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Containment Operator
+~~~~~~~~~~~~~~~~~~~~
The ``in`` operator performs containment test.
{# is equivalent to #}
{% if not (1 in [1, 2, 3]) %}
-Tests (new in Twig 0.9.9)
-~~~~~~~~~~~~~~~~~~~~~~~~~
+Tests
+~~~~~
The ``is`` operator performs tests. Tests can be used to test a variable against
a common expression. The right operand is name of the test:
The following operators are very useful but don't fit into any of the other
two categories:
-* ``..`` (new in Twig 0.9.5): Creates a sequence based on the operand before
- and after the operator (see the ``for`` tag for some usage examples).
+* ``..``: Creates a sequence based on the operand before and after the
+ operator (see the ``for`` tag for some usage examples).
* ``|``: Applies a filter.
{# returns I like foo and bar. (if the foo parameter equals to the foo string) #}
-``replace`` (new in Twig 0.9.9)
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+``replace``
+~~~~~~~~~~~
The ``replace`` filter formats a given string by replacing the placeholders
(placeholders are free-form):
{{ var|raw }} {# var won't be escaped #}
{% endautoescape %}
-``merge`` (new in Twig 0.9.10)
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+``merge``
+~~~~~~~~~
The ``merge`` filter merges an array or a hash with the value:
{# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}
-List of built-in Tests (new in Twig 0.9.9)
-------------------------------------------
+List of built-in Tests
+----------------------
``divisibleby``
~~~~~~~~~~~~~~~