Simplify accessing outer loop objects example

This commit is contained in:
Fabien Potencier
2024-07-28 10:31:49 +02:00
parent e19f056326
commit bc11e375f6
2 changed files with 41 additions and 40 deletions
-40
View File
@@ -223,46 +223,6 @@ thanks to the magic ``__get()`` method; you need to also implement the
} }
} }
Accessing the parent Context in Nested Loops
--------------------------------------------
Sometimes, when using nested loops, you need to access the parent context. The
parent context is always accessible via the ``loop.parent`` variable. For
instance, if you have the following template data::
$data = [
'topics' => [
'topic1' => ['Message 1 of topic 1', 'Message 2 of topic 1'],
'topic2' => ['Message 1 of topic 2', 'Message 2 of topic 2'],
],
];
And the following template to display all messages in all topics:
.. code-block:: twig
{% for topic, messages in topics %}
* {{ loop.index }}: {{ topic }}
{% for message in messages %}
- {{ loop.parent.loop.index }}.{{ loop.index }}: {{ message }}
{% endfor %}
{% endfor %}
The output will be similar to:
.. code-block:: text
* 1: topic1
- 1.1: The message 1 of topic 1
- 1.2: The message 2 of topic 1
* 2: topic2
- 2.1: The message 1 of topic 2
- 2.2: The message 2 of topic 2
In the inner loop, the ``loop.parent`` variable is used to access the outer
context. So, the index of the current ``topic`` defined in the outer for loop
is accessible via the ``loop.parent.loop.index`` variable.
Defining undefined Functions, Filters, and Tags on the Fly Defining undefined Functions, Filters, and Tags on the Fly
---------------------------------------------------------- ----------------------------------------------------------
+41
View File
@@ -216,3 +216,44 @@ Use ``loop.changed()`` to check if the value has changed since the last call:
{% endif %} {% endif %}
<p>{{ entry.message }}</p> <p>{{ entry.message }}</p>
{% endfor %} {% endfor %}
Accessing the outer ``loop`` in Nested Loops
--------------------------------------------
When using nested loops, you can access the outer ``loop`` object by storing it
in a variable before entering the inner loop.
For instance, if you have the following template data::
$data = [
'topics' => [
'topic1' => ['Message 1 of topic 1', 'Message 2 of topic 1'],
'topic2' => ['Message 1 of topic 2', 'Message 2 of topic 2'],
],
];
And the following template to display all messages in all topics:
.. code-block:: twig
{% for topic, messages in topics %}
* {{ loop.index }}: {{ topic }}
{% set outer_loop = loop %}
{% for message in messages %}
- {{ outer_loop.index }}.{{ loop.index }}: {{ message }}
{% endfor %}
{% endfor %}
The output will be similar to:
.. code-block:: text
* 1: topic1
- 1.1: The message 1 of topic 1
- 1.2: The message 2 of topic 1
* 2: topic2
- 2.1: The message 1 of topic 2
- 2.2: The message 2 of topic 2
Within the inner loop, the ``outer_loop`` variable can be used to reference the
outer loop object.