mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 04:16:28 +00:00
tweaked previous merge (refs #1103)
This commit is contained in:
+21
-12
@@ -52,18 +52,27 @@ The ``set`` tag can also be used to 'capture' chunks of text:
|
||||
|
||||
If you enable automatic output escaping, Twig will only consider the
|
||||
content to be safe when capturing chunks of text.
|
||||
|
||||
Note that differently from PHP in Twig loops are scoped, therefore a variable
|
||||
declared inside a for-in loop isn't accessibile outside the loop itself.
|
||||
The variable must be declared before the loop to be accessibile elsewhere.
|
||||
|
||||
.. code-block:: jinja
|
||||
.. note::
|
||||
|
||||
{% set foo = "" %}
|
||||
{% for item in list %}
|
||||
{% set foo = item %}
|
||||
{% endfor %}
|
||||
{{ dump(foo) }}
|
||||
|
||||
Note that loops are scoped in Twig; therefore a variable declared inside a
|
||||
``for`` loop is not accessible outside the loop itself:
|
||||
|
||||
|
||||
.. code-block:: jinja
|
||||
|
||||
{% for item in list %}
|
||||
{% set foo = item %}
|
||||
{% endfor %}
|
||||
|
||||
{# foo is NOT available #}
|
||||
|
||||
If you want to access the variable, just declare it before the loop:
|
||||
|
||||
.. code-block:: jinja
|
||||
|
||||
{% set foo = "" %}
|
||||
{% for item in list %}
|
||||
{% set foo = item %}
|
||||
{% endfor %}
|
||||
|
||||
{# foo is available #}
|
||||
|
||||
Reference in New Issue
Block a user