Optimized {% cycle %} template tag so that it creates the Variable objects in CycleNode.__init__() rather than each time render() is called
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7756 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
b7fea94096
commit
9ef491b028
|
@ -39,12 +39,11 @@ class CommentNode(Node):
|
||||||
|
|
||||||
class CycleNode(Node):
|
class CycleNode(Node):
|
||||||
def __init__(self, cyclevars, variable_name=None):
|
def __init__(self, cyclevars, variable_name=None):
|
||||||
self.cycle_iter = itertools_cycle(cyclevars)
|
self.cycle_iter = itertools_cycle([Variable(v) for v in cyclevars])
|
||||||
self.variable_name = variable_name
|
self.variable_name = variable_name
|
||||||
|
|
||||||
def render(self, context):
|
def render(self, context):
|
||||||
value = self.cycle_iter.next()
|
value = self.cycle_iter.next().resolve(context)
|
||||||
value = Variable(value).resolve(context)
|
|
||||||
if self.variable_name:
|
if self.variable_name:
|
||||||
context[self.variable_name] = value
|
context[self.variable_name] = value
|
||||||
return value
|
return value
|
||||||
|
@ -454,7 +453,7 @@ def cycle(parser, token):
|
||||||
<tr class="{% cycle rowcolors %}">...</tr>
|
<tr class="{% cycle rowcolors %}">...</tr>
|
||||||
<tr class="{% cycle rowcolors %}">...</tr>
|
<tr class="{% cycle rowcolors %}">...</tr>
|
||||||
|
|
||||||
You can use any number of values, seperated by spaces. Commas can also
|
You can use any number of values, separated by spaces. Commas can also
|
||||||
be used to separate values; if a comma is used, the cycle values are
|
be used to separate values; if a comma is used, the cycle values are
|
||||||
interpreted as literal strings.
|
interpreted as literal strings.
|
||||||
"""
|
"""
|
||||||
|
@ -464,7 +463,7 @@ def cycle(parser, token):
|
||||||
# one returned from {% cycle name %} are the exact same object. This
|
# one returned from {% cycle name %} are the exact same object. This
|
||||||
# shouldn't cause problems (heh), but if it does, now you know.
|
# shouldn't cause problems (heh), but if it does, now you know.
|
||||||
#
|
#
|
||||||
# Ugly hack warning: this stuffs the named template dict into parser so
|
# Ugly hack warning: This stuffs the named template dict into parser so
|
||||||
# that names are only unique within each template (as opposed to using
|
# that names are only unique within each template (as opposed to using
|
||||||
# a global variable, which would make cycle names have to be unique across
|
# a global variable, which would make cycle names have to be unique across
|
||||||
# *all* templates.
|
# *all* templates.
|
||||||
|
@ -483,8 +482,7 @@ def cycle(parser, token):
|
||||||
# {% cycle foo %} case.
|
# {% cycle foo %} case.
|
||||||
name = args[1]
|
name = args[1]
|
||||||
if not hasattr(parser, '_namedCycleNodes'):
|
if not hasattr(parser, '_namedCycleNodes'):
|
||||||
raise TemplateSyntaxError("No named cycles in template."
|
raise TemplateSyntaxError("No named cycles in template. '%s' is not defined" % name)
|
||||||
" '%s' is not defined" % name)
|
|
||||||
if not name in parser._namedCycleNodes:
|
if not name in parser._namedCycleNodes:
|
||||||
raise TemplateSyntaxError("Named cycle '%s' does not exist" % name)
|
raise TemplateSyntaxError("Named cycle '%s' does not exist" % name)
|
||||||
return parser._namedCycleNodes[name]
|
return parser._namedCycleNodes[name]
|
||||||
|
|
Loading…
Reference in New Issue