Ticket #2743: cycle.patch

File cycle.patch, 1.9 KB (added by martin.glueck@…, 9 years ago)

The patch for the tag enhancement and a new testcase for the new features

  • django/template/defaulttags.py

     
    1313        return ''
    1414
    1515class CycleNode(Node):
    16     def __init__(self, cyclevars):
     16    def __init__(self, cyclevars, variable_name = None):
    1717        self.cyclevars = cyclevars
    1818        self.cyclevars_len = len(cyclevars)
    1919        self.counter = -1
     20        self.variable_name = variable_name
    2021
    2122    def render(self, context):
    2223        self.counter += 1
    23         return self.cyclevars[self.counter % self.cyclevars_len]
     24        value = self.cyclevars[self.counter % self.cyclevars_len]
     25        if self.variable_name :
     26            context [self.variable_name] = value
     27        return value
    2428
    2529class DebugNode(Node):
    2630    def render(self, context):
     
    385389            raise TemplateSyntaxError("Second 'cycle' argument must be 'as'")
    386390        cyclevars = [v for v in args[1].split(",") if v]    # split and kill blanks
    387391        name = args[3]
    388         node = CycleNode(cyclevars)
     392        node = CycleNode(cyclevars, name)
    389393
    390394        if not hasattr(parser, '_namedCycleNodes'):
    391395            parser._namedCycleNodes = {}
  • tests/regressiontests/templates/tests.py

     
    187187            'cycle05': ('{% cycle %}', {}, template.TemplateSyntaxError),
    188188            'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError),
    189189            'cycle07': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, template.TemplateSyntaxError),
     190            'cycle08': ('{% cycle a,b,c as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}', {}, "abbbcc"),
    190191
    191192            ### EXCEPTIONS ############################################################
    192193
Back to Top