Skip to content
Myr

Added a dict iteration example. This fixes #64

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent b1b7b08 · Browse files · 1 file+16 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+16 added−2 removed

  • other:+16 added−2 removed

1 file+16 added−2 removed

modified:docs/templates.rst+16 added-2 removed
495 unmodified lines
496 <li>{{ user.username|e }}</li>497 {% endfor %}498 </ul>499500Inside of a for loop block you can access some special variables:500As variables in templates retain their object properties, it is possible to501iterate over containers like `dict`::502503 <dl>504 {% for key, value in my_dict.iteritems() %}505 <dt>{{ key|e }}</dt>506 <dd>{{ value|e }}</dd>507 {% endfor %}508 </dl>509510Note however that dictionaries usually are unordered so you might want to511either pass it as a sorted list to the template or use the `dictsort`512filter.513514Inside of a for-loop block you can access some special variables:515516+-----------------------+---------------------------------------------------+517| Variable | Description |518+=======================+===================================================+519| `loop.index` | The current iteration of the loop. (1 indexed) |
22 unmodified lines
542 {% for row in rows %}543 <li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>544 {% endfor %}545532With Jinja 2.1 an extra `cycle` helper exists that allows loop-unbound546Since Jinja 2.1 an extra `cycle` helper exists that allows loop-unbound547cycling. For more information have a look at the :ref:`builtin-globals`.548549.. _loop-filtering:550
830 unmodified lines