diff options
author | Armin Ronacher <armin.ronacher@active-4.com> | 2011-10-06 10:09:31 -0400 |
---|---|---|
committer | Armin Ronacher <armin.ronacher@active-4.com> | 2011-10-06 10:09:31 -0400 |
commit | 898975dd5d02fcbc13dbe19ff65a50b43820df1a (patch) | |
tree | 12984cedba49122d585cd55eddbbe8bbe41d0a8d /docs | |
parent | b1b7b0893ca2750c871b402ea556e31558dc09dc (diff) | |
download | jinja2-898975dd5d02fcbc13dbe19ff65a50b43820df1a.tar.gz |
Added a dict iteration example. This fixes #64
Diffstat (limited to 'docs')
-rw-r--r-- | docs/templates.rst | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/docs/templates.rst b/docs/templates.rst index 48429a5..3032e3a 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -497,7 +497,21 @@ provided in a variable called `users`:: {% 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 | @@ -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 %} -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: |