Update for.rst

Add context and explanation about loop.parent
This commit is contained in:
Marilena RUFFELAERE
2026-07-17 15:42:51 +02:00
committed by GitHub
parent da8bd40700
commit 7234d51ec8
+19
View File
@@ -80,6 +80,25 @@ Variable Description
``loop.last`` variables are only available for PHP arrays, or objects that
implement the ``Countable`` interface.
When loops are nested, you can access the parent loop context via the
``parent`` key of the ``loop`` variable:
.. code-block:: twig
{% for topic, messages in topics %}
* {{ loop.index }} : {{ topic }}
{% for message in messages %}
- {{ loop.parent.loop.index }}.{{ loop.index }}: {{ message }}
{% endfor %}
{% endfor %}
In the inner loop, ``loop.parent`` gives access to the context of the
enclosing loop. So, the index of the current topic (defined in the outer
``for`` loop) is available in the inner loop via
``loop.parent.loop.index``.
See also :doc:`the recipe on accessing the parent context in nested loops <recipes>`.
The ``else`` Clause
-------------------