Ticket #5756: 5756.2.patch
File 5756.2.patch, 6.2 KB (added by , 16 years ago) |
---|
-
django/template/defaulttags.py
38 38 return '' 39 39 40 40 class CycleNode(Node): 41 def __init__(self, cycleva rs, variable_name=None):42 self.cycle_iter = itertools_cycle(cycleva rs)41 def __init__(self, cyclevals, variable_name=None): 42 self.cycle_iter = itertools_cycle(cyclevals) 43 43 self.variable_name = variable_name 44 44 45 45 def render(self, context): 46 46 value = self.cycle_iter.next() 47 value = Variable(value).resolve(context)47 value = value.resolve(context, True) 48 48 if self.variable_name: 49 49 context[self.variable_name] = value 50 50 return value … … 70 70 return filtered 71 71 72 72 class FirstOfNode(Node): 73 def __init__(self, va rs):74 self.va rs = map(Variable, vars)73 def __init__(self, vals): 74 self.vals = vals 75 75 76 76 def render(self, context): 77 for var in self.vars: 78 try: 79 value = var.resolve(context) 80 except VariableDoesNotExist: 81 continue 77 for val in self.vals: 78 value = val.resolve(context, True) 82 79 if value: 83 80 return smart_unicode(value) 84 81 return u'' … … 158 155 return nodelist.render(context) 159 156 160 157 class IfChangedNode(Node): 161 def __init__(self, nodelist, *va rlist):158 def __init__(self, nodelist, *vallist): 162 159 self.nodelist = nodelist 163 160 self._last_seen = None 164 self._va rlist = map(Variable, varlist)161 self._vallist = vallist 165 162 166 163 def render(self, context): 167 164 if 'forloop' in context and context['forloop']['first']: 168 165 self._last_seen = None 169 166 try: 170 if self._va rlist:167 if self._vallist: 171 168 # Consider multiple parameters. This automatically behaves 172 169 # like an OR evaluation of the multiple variables. 173 compare_to = [va r.resolve(context) for var in self._varlist]170 compare_to = [val.resolve(context, True) for val in self._vallist] 174 171 else: 175 172 compare_to = self.nodelist.render(context) 176 173 except VariableDoesNotExist: … … 188 185 return '' 189 186 190 187 class IfEqualNode(Node): 191 def __init__(self, va r1, var2, nodelist_true, nodelist_false, negate):192 self.va r1, self.var2 = Variable(var1), Variable(var2)188 def __init__(self, val1, val2, nodelist_true, nodelist_false, negate): 189 self.val1, self.val2 = val1, val2 193 190 self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false 194 191 self.negate = negate 195 192 … … 197 194 return "<IfEqualNode>" 198 195 199 196 def render(self, context): 200 try: 201 val1 = self.var1.resolve(context) 202 except VariableDoesNotExist: 203 val1 = None 204 try: 205 val2 = self.var2.resolve(context) 206 except VariableDoesNotExist: 207 val2 = None 197 val1, val2 = (self.val1.resolve(context, True), self.val2.resolve(context, True)) 208 198 if (self.negate and val1 != val2) or (not self.negate and val1 == val2): 209 199 return self.nodelist_true.render(context) 210 200 return self.nodelist_false.render(context) … … 489 479 490 480 if len(args) > 4 and args[-2] == 'as': 491 481 name = args[-1] 492 node = CycleNode(args[1:-2], name) 482 values = [parser.compile_filter(arg) for arg in args[1:-2]] 483 node = CycleNode(values, name) 493 484 if not hasattr(parser, '_namedCycleNodes'): 494 485 parser._namedCycleNodes = {} 495 486 parser._namedCycleNodes[name] = node 496 487 else: 497 node = CycleNode(args[1:]) 488 values = [parser.compile_filter(arg) for arg in args[1:]] 489 node = CycleNode(values) 498 490 return node 499 491 cycle = register.tag(cycle) 500 492 … … 569 561 if len(bits) < 1: 570 562 raise TemplateSyntaxError("'firstof' statement requires at least one" 571 563 " argument") 572 return FirstOfNode(bits) 564 values = [parser.compile_filter(bit) for bit in bits] 565 return FirstOfNode(values) 573 566 firstof = register.tag(firstof) 574 567 575 568 #@register.tag(name="for") … … 640 633 if len(bits) != 3: 641 634 raise TemplateSyntaxError, "%r takes two arguments" % bits[0] 642 635 end_tag = 'end' + bits[0] 636 val1 = parser.compile_filter(bits[1]) 637 val2 = parser.compile_filter(bits[2]) 643 638 nodelist_true = parser.parse(('else', end_tag)) 644 639 token = parser.next_token() 645 640 if token.contents == 'else': … … 647 642 parser.delete_first_token() 648 643 else: 649 644 nodelist_false = NodeList() 650 return IfEqualNode( bits[1], bits[2], nodelist_true, nodelist_false, negate)645 return IfEqualNode(val1, val2, nodelist_true, nodelist_false, negate) 651 646 652 647 #@register.tag 653 648 def ifequal(parser, token): … … 803 798 bits = token.contents.split() 804 799 nodelist = parser.parse(('endifchanged',)) 805 800 parser.delete_first_token() 806 return IfChangedNode(nodelist, *bits[1:]) 801 values = [parser.compile_filter(bit) for bit in bits[1:]] 802 return IfChangedNode(nodelist, *values) 807 803 ifchanged = register.tag(ifchanged) 808 804 809 805 #@register.tag -
django/template/loader_tags.py
110 110 111 111 class IncludeNode(Node): 112 112 def __init__(self, template_name): 113 self.template_name = Variable(template_name)113 self.template_name = template_name 114 114 115 115 def render(self, context): 116 116 try: 117 template_name = self.template_name.resolve(context )117 template_name = self.template_name.resolve(context, True) 118 118 t = get_template(template_name) 119 119 return t.render(context) 120 120 except TemplateSyntaxError, e: … … 181 181 path = bits[1] 182 182 if path[0] in ('"', "'") and path[-1] == path[0]: 183 183 return ConstantIncludeNode(path[1:-1]) 184 return IncludeNode(bits[1]) 184 path_value = parser.compile_filter(path) 185 return IncludeNode(path_value) 185 186 186 187 register.tag('block', do_block) 187 188 register.tag('extends', do_extends)