Commit 9ef491b0 authored by Adrian Holovaty's avatar Adrian Holovaty
Browse files

Optimized {% cycle %} template tag so that it creates the Variable objects in...

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
parent b7fea940
Loading
Loading
Loading
Loading
+6 −8
Original line number Diff line number Diff line
@@ -39,12 +39,11 @@ class CommentNode(Node):

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,7 +453,7 @@ 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.
    """
@@ -464,7 +463,7 @@ def cycle(parser, token):
    # 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]