From 62516888f8f8c8790821eb0b872ca303b7337b91 Mon Sep 17 00:00:00 2001 From: fabien Date: Fri, 8 Jan 2010 12:48:59 +0000 Subject: [PATCH] moved the nested loop doc into a recipe git-svn-id: http://svn.twig-project.org/trunk@214 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- doc/02-Twig-for-Template-Designers.markdown | 32 ----------------------- doc/06-Recipes.markdown | 37 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index 906bdaf..5747696 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -487,38 +487,6 @@ You can also access both keys and values: >On Twig before 0.9.3, you need to use the `items` filter to access both the >keys and values (`{% for key, value in users|items %}`). -When using nested loops, the parent context is accessible via the -`loop.parent` variable. For instance, if you have the following template data: - - $data = array( - 'topics' => array( - 'topic1' => array('Message 1 of topic 1', 'Message 2 of topic 1'), - 'topic2' => array('Message 1 of topic 2', 'Message 2 of topic 2'), - ), - ); - -And the following template to display all messages in all topics: - - [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: - - * 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, we use the `loop.parent` to access the outer context. So, -the index of the `topics` for loop is accessible via `loop.parent.loop.index`. - ### If The `if` statement in Twig is comparable with the if statements of PHP. In the diff --git a/doc/06-Recipes.markdown b/doc/06-Recipes.markdown index 07a4042..1b66b35 100644 --- a/doc/06-Recipes.markdown +++ b/doc/06-Recipes.markdown @@ -174,3 +174,40 @@ allow communication between your templates and your application: Now, you can use the setter to inject the context whenever you create a template, and use the getter from within your custom nodes. + +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 = array( + 'topics' => array( + 'topic1' => array('Message 1 of topic 1', 'Message 2 of topic 1'), + 'topic2' => array('Message 1 of topic 2', 'Message 2 of topic 2'), + ), + ); + +And the following template to display all messages in all topics: + + [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: + + * 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. -- 1.7.2.5