git-svn-id: http://svn.twig-project.org/trunk@214 93ef8e89-cb99-4229-a87c-7fa0fa45744b
... | ... |
@@ -487,38 +487,6 @@ You can also access both keys and values: |
487 | 487 |
>On Twig before 0.9.3, you need to use the `items` filter to access both the |
488 | 488 |
>keys and values (`{% for key, value in users|items %}`). |
489 | 489 |
|
490 |
-When using nested loops, the parent context is accessible via the |
|
491 |
-`loop.parent` variable. For instance, if you have the following template data: |
|
492 |
- |
|
493 |
- $data = array( |
|
494 |
- 'topics' => array( |
|
495 |
- 'topic1' => array('Message 1 of topic 1', 'Message 2 of topic 1'), |
|
496 |
- 'topic2' => array('Message 1 of topic 2', 'Message 2 of topic 2'), |
|
497 |
- ), |
|
498 |
- ); |
|
499 |
- |
|
500 |
-And the following template to display all messages in all topics: |
|
501 |
- |
|
502 |
- [twig] |
|
503 |
- {% for topic, messages in topics %} |
|
504 |
- * {{ loop.index }}: {{ topic }} |
|
505 |
- {% for message in messages %} |
|
506 |
- - {{ loop.parent.loop.index }}.{{ loop.index }}: {{ message }} |
|
507 |
- {% endfor %} |
|
508 |
- {% endfor %} |
|
509 |
- |
|
510 |
-The output will be similar to: |
|
511 |
- |
|
512 |
- * 1: topic1 |
|
513 |
- - 1.1: The message 1 of topic 1 |
|
514 |
- - 1.2: The message 2 of topic 1 |
|
515 |
- * 2: topic2 |
|
516 |
- - 2.1: The message 1 of topic 2 |
|
517 |
- - 2.2: The message 2 of topic 2 |
|
518 |
- |
|
519 |
-In the inner loop, we use the `loop.parent` to access the outer context. So, |
|
520 |
-the index of the `topics` for loop is accessible via `loop.parent.loop.index`. |
|
521 |
- |
|
522 | 490 |
### If |
523 | 491 |
|
524 | 492 |
The `if` statement in Twig is comparable with the if statements of PHP. In the |
... | ... |
@@ -174,3 +174,40 @@ allow communication between your templates and your application: |
174 | 174 |
|
175 | 175 |
Now, you can use the setter to inject the context whenever you create a |
176 | 176 |
template, and use the getter from within your custom nodes. |
177 |
+ |
|
178 |
+Accessing the parent Context in Nested Loops |
|
179 |
+-------------------------------------------- |
|
180 |
+ |
|
181 |
+Sometimes, when using nested loops, you need to access the parent context. The |
|
182 |
+parent context is always accessible via the `loop.parent` variable. For |
|
183 |
+instance, if you have the following template data: |
|
184 |
+ |
|
185 |
+ $data = array( |
|
186 |
+ 'topics' => array( |
|
187 |
+ 'topic1' => array('Message 1 of topic 1', 'Message 2 of topic 1'), |
|
188 |
+ 'topic2' => array('Message 1 of topic 2', 'Message 2 of topic 2'), |
|
189 |
+ ), |
|
190 |
+ ); |
|
191 |
+ |
|
192 |
+And the following template to display all messages in all topics: |
|
193 |
+ |
|
194 |
+ [twig] |
|
195 |
+ {% for topic, messages in topics %} |
|
196 |
+ * {{ loop.index }}: {{ topic }} |
|
197 |
+ {% for message in messages %} |
|
198 |
+ - {{ loop.parent.loop.index }}.{{ loop.index }}: {{ message }} |
|
199 |
+ {% endfor %} |
|
200 |
+ {% endfor %} |
|
201 |
+ |
|
202 |
+The output will be similar to: |
|
203 |
+ |
|
204 |
+ * 1: topic1 |
|
205 |
+ - 1.1: The message 1 of topic 1 |
|
206 |
+ - 1.2: The message 2 of topic 1 |
|
207 |
+ * 2: topic2 |
|
208 |
+ - 2.1: The message 1 of topic 2 |
|
209 |
+ - 2.2: The message 2 of topic 2 |
|
210 |
+ |
|
211 |
+In the inner loop, the `loop.parent` variable is used to access the outer |
|
212 |
+context. So, the index of the current `topic` defined in the outer for loop is |
|
213 |
+accessible via the `loop.parent.loop.index` variable. |