Ticket #7501: fixcyclebehaviour.patch

File fixcyclebehaviour.patch, 2.5 KB (added by Stephen Kelly, 15 years ago)

Fixes cycle reset issue by adding reset feature to node.

  • tests/regressiontests/templates/tests.py

     
    156156        split = token.split_contents()
    157157        self.assertEqual(split, ["sometag", '_("Page not found")', 'value|yesno:_("yes,no")'])
    158158
     159    def test_cycle_multiple_render(self):
     160        t = template.Template("{% cycle a,b,c as abc %}{% cycle abc %}")
     161        c = template.Context({})
     162        print "K", t.render(c), t.render(c)
     163        self.assertEqual(t.render(c),  t.render(c))
     164
     165        t = template.Template("{% for letter in abc %}{% cycle 'd' 'e' 'f' 'g'  %}{% endfor %}")
     166        c = template.Context({"abc": ("a", "b", "c")})
     167
     168        print "S", t.render(c), t.render(c)
     169        self.assertEqual(t.render(c),  t.render(c))
     170
    159171    def test_url_reverse_no_settings_module(self):
    160172        # Regression test for #9005
    161173        from django.template import Template, Context, TemplateSyntaxError
  • django/template/defaulttags.py

     
    3939
    4040class CycleNode(Node):
    4141    def __init__(self, cyclevars, variable_name=None):
    42         self.cycle_iter = itertools_cycle(cyclevars)
     42        self.cycle_vars = cyclevars
    4343        self.variable_name = variable_name
     44        self._first = False
    4445
    4546    def render(self, context):
     47        if self._first:
     48            self.cycle_iter = itertools_cycle(self.cycle_vars)
    4649        value = self.cycle_iter.next().resolve(context)
    4750        if self.variable_name:
    4851            context[self.variable_name] = value
     52        self._first = False
    4953        return value
    5054
     55    def reset(self):
     56        self._first = True
     57
    5158class DebugNode(Node):
    5259    def render(self, context):
    5360        from pprint import pformat
  • django/template/__init__.py

     
    175175
    176176    def render(self, context):
    177177        "Display stage -- can be called many times"
     178        for n in self.nodelist.get_nodes_by_type(Node):
     179            try:
     180                n.reset()
     181            except:
     182                pass
    178183        return self.nodelist.render(context)
    179184
    180185def compile_string(template_string, origin):
Back to Top