diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2007-06-08 11:58:03 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2007-06-08 11:58:03 +0000 |
| commit | 16269c4d0a5d2e61c7555fec438440abee9be9f5 (patch) | |
| tree | 7a72ef1954e5b918ce13049220a883dd2f15fbc4 /docs | |
| parent | 83fe33e2774b9ae59007f39ef61abce73e09c54f (diff) | |
Fixed #3523 -- Added list unpacking to for loops in templates. Thanks to SmileyChris and Honza Kral for their work.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5443 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/templates.txt | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/docs/templates.txt b/docs/templates.txt index d8b511dedc..cb8e238f43 100644 --- a/docs/templates.txt +++ b/docs/templates.txt @@ -447,7 +447,7 @@ for ~~~ Loop over each item in an array. For example, to display a list of athletes -given ``athlete_list``:: +provided in ``athlete_list``:: <ul> {% for athlete in athlete_list %} @@ -455,7 +455,25 @@ given ``athlete_list``:: {% endfor %} </ul> -You can also loop over a list in reverse by using ``{% for obj in list reversed %}``. +You can loop over a list in reverse by using ``{% for obj in list reversed %}``. + +**New in Django development version** +If you need to loop over a list of lists, you can unpack the values +in eachs sub-list into a set of known names. For example, if your context contains +a list of (x,y) coordinates called ``points``, you could use the following +to output the list of points:: + + {% for x, y in points %} + There is a point at {{ x }},{{ y }} + {% endfor %} + +This can also be useful if you need to access the items in a dictionary. +For example, if your context contained a dictionary ``data``, the following +would display the keys and values of the dictionary:: + + {% for key, value in data.items %} + {{ key }}: {{ value }} + {% endfor %} The for loop sets a number of variables available within the loop: |
