From 293f0f2360f568896c1788335c055b1582d19f9a Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Wed, 2 Jan 2008 05:01:03 +0000 Subject: [PATCH] Fixed #6295 -- Made the {% for %} tag a bit more efficient by creating a single context dictionary rather than recreating it each time through the loop. Thanks, Ned Batchelder git-svn-id: http://code.djangoproject.com/svn/django/trunk@6981 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/defaulttags.py | 25 ++++++++++++------------ tests/regressiontests/templates/tests.py | 2 ++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 36b07fa7b3..726a37012b 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -125,19 +125,20 @@ class ForNode(Node): if self.is_reversed: values = reversed(values) unpack = len(self.loopvars) > 1 + # Create a forloop value in the context. We'll update counters on each + # iteration just below. + loop_dict = context['forloop'] = {'parentloop': parentloop} for i, item in enumerate(values): - context['forloop'] = { - # 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), - 'parentloop': parentloop, - } + # Shortcuts for current loop iteration number. + loop_dict['counter0'] = i + loop_dict['counter'] = i+1 + # Reverse counter iteration numbers. + loop_dict['revcounter'] = len_values - i + loop_dict['revcounter0'] = len_values - i - 1 + # Boolean values designating first and last times through loop. + loop_dict['first'] = (i == 0) + loop_dict['last'] = (i == len_values - 1) + if unpack: # If there are multiple loop variables, unpack the item into # them. diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index bbe3902f5b..ef8fd43292 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -441,6 +441,8 @@ class Templates(unittest.TestCase): '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"), + 'for-tag-vars05': ("{% for val in values %}{% if forloop.first %}f{% else %}x{% endif %}{% endfor %}", {"values": [6, 6, 6]}, "fxx"), + 'for-tag-vars06': ("{% for val in values %}{% if forloop.last %}l{% else %}x{% endif %}{% endfor %}", {"values": [6, 6, 6]}, "xxl"), 'for-tag-unpack01': ("{% for key,value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"), 'for-tag-unpack03': ("{% for key, value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"), 'for-tag-unpack04': ("{% for key , value in items %}{{ key }}:{{ value }}/{% endfor %}", {"items": (('one', 1), ('two', 2))}, "one:1/two:2/"),