Django

Code

Changeset 7756

Show
Ignore:
Timestamp:
06/26/08 00:16:19 (5 months ago)
Author:
adrian
Message:

Optimized {% cycle %} template tag so that it creates the Variable objects in CycleNode?.init() rather than each time render() is called

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/template/defaulttags.py

    r7752 r7756  
    4040class CycleNode(Node): 
    4141    def __init__(self, cyclevars, variable_name=None): 
    42         self.cycle_iter = itertools_cycle(cyclevars
     42        self.cycle_iter = itertools_cycle([Variable(v) for v in cyclevars]
    4343        self.variable_name = variable_name 
    4444 
    4545    def render(self, context): 
    46         value = self.cycle_iter.next() 
    47         value = Variable(value).resolve(context) 
     46        value = self.cycle_iter.next().resolve(context) 
    4847        if self.variable_name: 
    4948            context[self.variable_name] = value 
     
    455454            <tr class="{% cycle rowcolors %}">...</tr> 
    456455 
    457     You can use any number of values, seperated by spaces. Commas can also 
     456    You can use any number of values, separated by spaces. Commas can also 
    458457    be used to separate values; if a comma is used, the cycle values are 
    459458    interpreted as literal strings. 
     
    462461    # Note: This returns the exact same node on each {% cycle name %} call; 
    463462    # that is, the node object returned from {% cycle a b c as name %} and the 
    464     # one returned from {% cycle name %} are the exact same object. This 
     463    # one returned from {% cycle name %} are the exact same object. This 
    465464    # shouldn't cause problems (heh), but if it does, now you know. 
    466465    # 
    467     # Ugly hack warning: this stuffs the named template dict into parser so 
     466    # Ugly hack warning: This stuffs the named template dict into parser so 
    468467    # that names are only unique within each template (as opposed to using 
    469468    # a global variable, which would make cycle names have to be unique across 
     
    484483        name = args[1] 
    485484        if not hasattr(parser, '_namedCycleNodes'): 
    486             raise TemplateSyntaxError("No named cycles in template." 
    487                                       " '%s' is not defined" % name) 
     485            raise TemplateSyntaxError("No named cycles in template. '%s' is not defined" % name) 
    488486        if not name in parser._namedCycleNodes: 
    489487            raise TemplateSyntaxError("Named cycle '%s' does not exist" % name)