diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2005-09-29 23:34:29 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2005-09-29 23:34:29 +0000 |
| commit | 27b1f69d79787af2c2db35b4d2a96784a59d39a7 (patch) | |
| tree | db432f2a872cc4d9034c6afd7e1e7e62b11823fd | |
| parent | 151b44c89a6c53bc5e312fd84afeedffb23a0e94 (diff) | |
Fixed #295 - added {{{forloop.revcounter}}} and {{{forloop.revcounter0}}} variables to for loops. Also updated the docs and added unit tests to verify correct behavior. Thanks, Clint.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@736 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/core/defaulttags.py | 7 | ||||
| -rw-r--r-- | docs/templates.txt | 4 | ||||
| -rw-r--r-- | tests/othertests/templates.py | 4 |
3 files changed, 15 insertions, 0 deletions
diff --git a/django/core/defaulttags.py b/django/core/defaulttags.py index 103eb4c9f4..e86b385f9c 100644 --- a/django/core/defaulttags.py +++ b/django/core/defaulttags.py @@ -97,6 +97,9 @@ class ForNode(template.Node): # shortcuts for current loop iteration number 'counter0': i, 'counter': i+1, + # reverse counter iteration numbers + 'revcounter': len_values - i, + 'revcounter0': len_values - i - 1, # boolean values designating first and last times through loop 'first': (i == 0), 'last': (i == len_values - 1), @@ -431,6 +434,10 @@ def do_for(parser, token): ========================== ================================================ ``forloop.counter`` The current iteration of the loop (1-indexed) ``forloop.counter0`` The current iteration of the loop (0-indexed) + ``forloop.revcounter`` The number of iterations from the end of the + loop (1-indexed) + ``forloop.revcounter0`` The number of iterations from the end of the + loop (0-indexed) ``forloop.first`` True if this is the first time through the loop ``forloop.last`` True if this is the last time through the loop ``forloop.parentloop`` For nested loops, this is the loop "above" the diff --git a/docs/templates.txt b/docs/templates.txt index 09431c1dda..a6848a9638 100644 --- a/docs/templates.txt +++ b/docs/templates.txt @@ -376,6 +376,10 @@ Built-in tag reference ========================== ================================================ ``forloop.counter`` The current iteration of the loop (1-indexed) ``forloop.counter0`` The current iteration of the loop (0-indexed) + ``forloop.revcounter`` The number of iterations from the end of the + loop (1-indexed) + ``forloop.revcounter0`` The number of iterations from the end of the + loop (0-indexed) ``forloop.first`` True if this is the first time through the loop ``forloop.last`` True if this is the last time through the loop ``forloop.parentloop`` For nested loops, this is the loop "above" the diff --git a/tests/othertests/templates.py b/tests/othertests/templates.py index 31fea0e1ba..fb96cfeadd 100644 --- a/tests/othertests/templates.py +++ b/tests/othertests/templates.py @@ -107,6 +107,10 @@ TEMPLATE_TESTS = { ### FOR TAG ############################################################### 'for-tag01': ("{% for val in values %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "123"), 'for-tag02': ("{% for val in values reversed %}{{ val }}{% endfor %}", {"values": [1, 2, 3]}, "321"), + 'for-tag-vars01': ("{% for val in values %}{{ forloop.counter }}{% endfor %}", {"values": [6, 6, 6]}, "123"), + 'for-tag-vars02': ("{% for val in values %}{{ forloop.counter0 }}{% endfor %}", {"values": [6, 6, 6]}, "012"), + 'for-tag-vars03': ("{% for val in values %}{{ forloop.revcounter }}{% endfor %}", {"values": [6, 6, 6]}, "321"), + 'for-tag-vars04': ("{% for val in values %}{{ forloop.revcounter0 }}{% endfor %}", {"values": [6, 6, 6]}, "210"), ### IFEQUAL TAG ########################################################### 'ifequal01': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 2}, ""), |
