Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2743 -- Made the value of a cycle tag accessible through the c…

…ontext.

Patch from Martin Glueck.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@3773 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d296e5e565fff0d5b8ff9ed766919cf55904b79a 1 parent d85ee1c
Malcolm Tredinnick authored September 21, 2006
10  django/template/defaulttags.py
@@ -13,14 +13,18 @@ def render(self, context):
13 13
         return ''
14 14
 
15 15
 class CycleNode(Node):
16  
-    def __init__(self, cyclevars):
  16
+    def __init__(self, cyclevars, variable_name=None):
17 17
         self.cyclevars = cyclevars
18 18
         self.cyclevars_len = len(cyclevars)
19 19
         self.counter = -1
  20
+        self.variable_name = variable_name
20 21
 
21 22
     def render(self, context):
22 23
         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
24 28
 
25 29
 class DebugNode(Node):
26 30
     def render(self, context):
@@ -385,7 +389,7 @@ def cycle(parser, token):
385 389
             raise TemplateSyntaxError("Second 'cycle' argument must be 'as'")
386 390
         cyclevars = [v for v in args[1].split(",") if v]    # split and kill blanks
387 391
         name = args[3]
388  
-        node = CycleNode(cyclevars)
  392
+        node = CycleNode(cyclevars, name)
389 393
 
390 394
         if not hasattr(parser, '_namedCycleNodes'):
391 395
             parser._namedCycleNodes = {}
1  tests/regressiontests/templates/tests.py
@@ -187,6 +187,7 @@ def test_templates(self):
187 187
             'cycle05': ('{% cycle %}', {}, template.TemplateSyntaxError),
188 188
             'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError),
189 189
             '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'),
190 191
 
191 192
             ### EXCEPTIONS ############################################################
192 193
 

0 notes on commit d296e5e

Please sign in to comment.
Something went wrong with that request. Please try again.