Added a dict iteration example. This fixes #64
authorArmin Ronacher <armin.ronacher@active-4.com>
Thu, 6 Oct 2011 14:09:31 +0000 (10:09 -0400)
committerArmin Ronacher <armin.ronacher@active-4.com>
Thu, 6 Oct 2011 14:09:31 +0000 (10:09 -0400)
docs/templates.rst

index 48429a5880396f7add90bbedd286e272e79827db..3032e3a6ac6a39de57b8d5e235cbb57878579684 100644 (file)
@@ -497,7 +497,21 @@ provided in a variable called `users`::
     {% endfor %}
     </ul>
 
     {% endfor %}
     </ul>
 
-Inside of a for loop block you can access some special variables:
+As variables in templates retain their object properties, it is possible to
+iterate over containers like `dict`::
+
+    <dl>
+    {% for key, value in my_dict.iteritems() %}
+        <dt>{{ key|e }}</dt>
+        <dd>{{ value|e }}</dd>
+    {% endfor %}
+    </dl>
+
+Note however that dictionaries usually are unordered so you might want to
+either pass it as a sorted list to the template or use the `dictsort`
+filter.
+
+Inside of a for-loop block you can access some special variables:
 
 +-----------------------+---------------------------------------------------+
 | Variable              | Description                                       |
 
 +-----------------------+---------------------------------------------------+
 | Variable              | Description                                       |
@@ -529,7 +543,7 @@ each time through the loop by using the special `loop.cycle` helper::
         <li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>
     {% endfor %}
 
         <li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>
     {% endfor %}
 
-With Jinja 2.1 an extra `cycle` helper exists that allows loop-unbound
+Since Jinja 2.1 an extra `cycle` helper exists that allows loop-unbound
 cycling.  For more information have a look at the :ref:`builtin-globals`.
 
 .. _loop-filtering:
 cycling.  For more information have a look at the :ref:`builtin-globals`.
 
 .. _loop-filtering: