From d296e5e565fff0d5b8ff9ed766919cf55904b79a Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Thu, 21 Sep 2006 13:09:00 +0000 Subject: [PATCH] Fixed #2743 -- Made the value of a cycle tag accessible through the context. Patch from Martin Glueck. git-svn-id: http://code.djangoproject.com/svn/django/trunk@3773 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/defaulttags.py | 10 +++++++--- tests/regressiontests/templates/tests.py | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 691b40f3324..daf7f41a85a 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -13,14 +13,18 @@ class CommentNode(Node): 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 = {} diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 5a8dd2d6a21..b4ecb476ef7 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -187,6 +187,7 @@ class Templates(unittest.TestCase): '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 ############################################################