~~~~~~~~~~~~~~~~~~
If automatic escaping is enabled, the output of the filter may be escaped
-before printing. If your filter acts as an escaper (or explicitly outputs html
+before printing. If your filter acts as an escaper (or explicitly outputs HTML
or JavaScript code), you will want the raw output to be printed. In such a
case, set the ``is_safe`` option::
$filter = new Twig_SimpleFilter('nl2br', 'nl2br', array('is_safe' => array('html')));
Some filters may need to work on input that is already escaped or safe, for
-example when adding (safe) html tags to originally unsafe output. In such a
+example when adding (safe) HTML tags to originally unsafe output. In such a
case, set the ``pre_escape`` option to escape the input data before it is run
through your filter::
$twig->addTest($test);
Tests allow you to create custom application specific logic for evaluating
-boolean conditions. As a simple, example let's create a Twig test that checks if
+boolean conditions. As a simple example, let's create a Twig test that checks if
objects are 'red'::
$twig = new Twig_Environment($loader)
~~~~~~~~~~~~~~~~~~
If automatic escaping is enabled, the output of the filter may be escaped
-before printing. If your filter acts as an escaper (or explicitly outputs html
-or javascript code), you will want the raw output to be printed. In such a
+before printing. If your filter acts as an escaper (or explicitly outputs HTML
+or JavaScript code), you will want the raw output to be printed. In such a
case, set the ``is_safe`` option::
$filter = new Twig_Filter_Function('nl2br', array('is_safe' => array('html')));
Some filters may need to work on input that is already escaped or safe, for
-example when adding (safe) html tags to originally unsafe output. In such a
+example when adding (safe) HTML tags to originally unsafe output. In such a
case, set the ``pre_escape`` option to escape the input data before it is run
through your filter::
{{ var|upper|raw }} {# won't be escaped #}
* Automatic escaping is not applied if the last filter in the chain is marked
- safe for the current context (e.g. ``html`` or ``js``). ``escaper`` and
- ``escaper('html')`` are marked safe for html, ``escaper('js')`` is marked
- safe for javascript, ``raw`` is marked safe for everything.
+ safe for the current context (e.g. ``html`` or ``js``). ``escape`` and
+ ``escape('html')`` are marked safe for HTML, ``escape('js')`` is marked
+ safe for JavaScript, ``raw`` is marked safe for everything.
.. code-block:: jinja
{% autoescape 'js' %}
- {{ var|escape('html') }} {# will be escaped for html and javascript #}
- {{ var }} {# will be escaped for javascript #}
+ {{ var|escape('html') }} {# will be escaped for HTML and JavaScript #}
+ {{ var }} {# will be escaped for JavaScript #}
{{ var|escape('js') }} {# won't be double-escaped #}
{% endautoescape %}
{{ "I like %s and %s."|format(foo, "bar") }}
- {# returns I like foo and bar
+ {# outputs I like foo and bar
if the foo parameter equals to the foo string. #}
.. _`sprintf`: http://www.php.net/sprintf
.. code-block:: jinja
{{ [1, 2, 3]|join('|') }}
- {# returns 1|2|3 #}
+ {# outputs 1|2|3 #}
Arguments
---------
.. code-block:: jinja
- {% autoescape true %}
+ {% autoescape %}
{{ var|raw }} {# var won't be escaped #}
{% endautoescape %}
{{ "I like %this% and %that%."|replace({'%this%': foo, '%that%': "bar"}) }}
- {# returns I like foo and bar
+ {# outputs I like foo and bar
if the foo parameter equals to the foo string. #}
Arguments
{{ random(['apple', 'orange', 'citrus']) }} {# example output: orange #}
{{ random('ABC') }} {# example output: C #}
- {{ random() }} {# example output: 15386094 (works as native PHP `mt_rand`_ function) #}
+ {{ random() }} {# example output: 15386094 (works as the native PHP mt_rand function) #}
{{ random(5) }} {# example output: 3 #}
Arguments
{{ i }},
{% endfor %}
- {# returns 0, 1, 2, 3 #}
+ {# outputs 0, 1, 2, 3, #}
When step is given (as the third parameter), it specifies the increment (or
decrement):
{{ i }},
{% endfor %}
- {# returns 0, 2, 4, 6 #}
+ {# outputs 0, 2, 4, 6, #}
The Twig built-in ``..`` operator is just syntactic sugar for the ``range``
function (with a step of 1):
==========
.. versionadded:: 1.15
- The include function was added in Twig 1.15.
+ The source function was added in Twig 1.15.
The ``source`` function returns the content of a template without rendering it:
Installing via Composer (recommended)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-1. Install composer in your project:
+1. Install Composer in your project:
.. code-block:: bash
}
}
-3. Install via composer
+3. Install via Composer
.. code-block:: bash
But if you have many HTML and JS files, and if your template names follow some
conventions, you can instead determine the default escaping strategy to use
-based on the template name. Let's say that your template names always ends
+based on the template name. Let's say that your template names always end
with ``.html`` for HTML files, ``.js`` for JavaScript ones, and ``.css`` for
stylesheets, here is how you can configure Twig::
{% block content %}{% endblock %}
The ``use`` statement tells Twig to import the blocks defined in
-```blocks.html`` into the current template (it's like macros, but for blocks):
+``blocks.html`` into the current template (it's like macros, but for blocks):
.. code-block:: jinja
.. code-block:: jinja
{% if foo.attribute is same as(false) %}
- the foo attribute really is the ``false`` PHP value
+ the foo attribute really is the 'false' PHP value
{% endif %}