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
This commit is contained in:
Malcolm Tredinnick 2006-09-21 13:09:00 +00:00
parent d85ee1c01e
commit d296e5e565
2 changed files with 8 additions and 3 deletions

View File

@ -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 = {}

View File

@ -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 ############################################################