Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Optimized {% cycle %} template tag so that it creates the Variable ob…

…jects 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
  • Loading branch information...
commit 9ef491b0281e74ec975c536b06521fcb9e7a0b88 1 parent b7fea94
@adrianholovaty adrianholovaty authored
Showing with 6 additions and 8 deletions.
  1. +6 −8 django/template/defaulttags.py
View
14 django/template/defaulttags.py
@@ -39,12 +39,11 @@ def render(self, context):
class CycleNode(Node):
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
def render(self, context):
- value = self.cycle_iter.next()
- value = Variable(value).resolve(context)
+ value = self.cycle_iter.next().resolve(context)
if self.variable_name:
context[self.variable_name] = value
return value
@@ -454,17 +453,17 @@ def cycle(parser, token):
<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
interpreted as literal strings.
"""
# Note: This returns the exact same node on each {% cycle name %} call;
# that is, the node object returned from {% cycle a b c as name %} and the
- # 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.
#
- # 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
# a global variable, which would make cycle names have to be unique across
# *all* templates.
@@ -483,8 +482,7 @@ def cycle(parser, token):
# {% cycle foo %} case.
name = args[1]
if not hasattr(parser, '_namedCycleNodes'):
- raise TemplateSyntaxError("No named cycles in template."
- " '%s' is not defined" % name)
+ raise TemplateSyntaxError("No named cycles in template. '%s' is not defined" % name)
if not name in parser._namedCycleNodes:
raise TemplateSyntaxError("Named cycle '%s' does not exist" % name)
return parser._namedCycleNodes[name]
Please sign in to comment.
Something went wrong with that request. Please try again.