Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
@malcolmt malcolmt authored
View
10 django/template/defaulttags.py
@@ -13,14 +13,18 @@ def render(self, context):
return ''
class CycleNode(Node):
- def __init__(self, cyclevars):
+ def __init__(self, cyclevars, variable_name=None):
self.cyclevars = cyclevars
self.cyclevars_len = len(cyclevars)
self.counter = -1
+ self.variable_name = variable_name
def render(self, context):
self.counter += 1
- return self.cyclevars[self.counter % self.cyclevars_len]
+ value = self.cyclevars[self.counter % self.cyclevars_len]
+ if self.variable_name:
+ context[self.variable_name] = value
+ return value
class DebugNode(Node):
def render(self, context):
@@ -385,7 +389,7 @@ def cycle(parser, token):
raise TemplateSyntaxError("Second 'cycle' argument must be 'as'")
cyclevars = [v for v in args[1].split(",") if v] # split and kill blanks
name = args[3]
- node = CycleNode(cyclevars)
+ node = CycleNode(cyclevars, name)
if not hasattr(parser, '_namedCycleNodes'):
parser._namedCycleNodes = {}
View
1  tests/regressiontests/templates/tests.py
@@ -187,6 +187,7 @@ def test_templates(self):
'cycle05': ('{% cycle %}', {}, template.TemplateSyntaxError),
'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError),
'cycle07': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, template.TemplateSyntaxError),
+ 'cycle08': ('{% cycle a,b,c as foo %}{% cycle foo %}{{ foo }}{{ foo }}{% cycle foo %}{{ foo }}', {}, 'abbbcc'),
### EXCEPTIONS ############################################################
Please sign in to comment.
Something went wrong with that request. Please try again.