mirror of https://github.com/django/django.git
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:
parent
d85ee1c01e
commit
d296e5e565
|
@ -13,14 +13,18 @@ class CommentNode(Node):
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
class CycleNode(Node):
|
class CycleNode(Node):
|
||||||
def __init__(self, cyclevars):
|
def __init__(self, cyclevars, variable_name=None):
|
||||||
self.cyclevars = cyclevars
|
self.cyclevars = cyclevars
|
||||||
self.cyclevars_len = len(cyclevars)
|
self.cyclevars_len = len(cyclevars)
|
||||||
self.counter = -1
|
self.counter = -1
|
||||||
|
self.variable_name = variable_name
|
||||||
|
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
self.counter += 1
|
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):
|
class DebugNode(Node):
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
|
@ -385,7 +389,7 @@ def cycle(parser, token):
|
||||||
raise TemplateSyntaxError("Second 'cycle' argument must be 'as'")
|
raise TemplateSyntaxError("Second 'cycle' argument must be 'as'")
|
||||||
cyclevars = [v for v in args[1].split(",") if v] # split and kill blanks
|
cyclevars = [v for v in args[1].split(",") if v] # split and kill blanks
|
||||||
name = args[3]
|
name = args[3]
|
||||||
node = CycleNode(cyclevars)
|
node = CycleNode(cyclevars, name)
|
||||||
|
|
||||||
if not hasattr(parser, '_namedCycleNodes'):
|
if not hasattr(parser, '_namedCycleNodes'):
|
||||||
parser._namedCycleNodes = {}
|
parser._namedCycleNodes = {}
|
||||||
|
|
|
@ -187,6 +187,7 @@ class Templates(unittest.TestCase):
|
||||||
'cycle05': ('{% cycle %}', {}, template.TemplateSyntaxError),
|
'cycle05': ('{% cycle %}', {}, template.TemplateSyntaxError),
|
||||||
'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError),
|
'cycle06': ('{% cycle a %}', {}, template.TemplateSyntaxError),
|
||||||
'cycle07': ('{% cycle a,b,c as foo %}{% cycle bar %}', {}, 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 ############################################################
|
### EXCEPTIONS ############################################################
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue