Ticket #4552: for_tag_tidy.patch
File for_tag_tidy.patch, 2.3 KB (added by , 17 years ago) |
---|
-
django/template/defaulttags.py
5 5 from django.template import get_library, Library, InvalidTemplateLibrary 6 6 from django.conf import settings 7 7 import sys 8 from itertools import izip 8 9 import re 9 10 10 11 if not hasattr(__builtins__, 'reversed'): … … 127 128 'parentloop': parentloop, 128 129 } 129 130 if unpack: 130 # If there are multiple loop variables, unpack the item into them. 131 context.update(dict(zip(self.loopvars, item))) 131 # If there are multiple loop variables, unpack the item into the 132 # current context scope. 133 for key, value in izip(self.loopvars, item): 134 context[key] = value 135 # Remove any unused loopvars from the current context scope 136 # which could have been left around from the last loop. 137 for key in self.loopvars[len(item):]: 138 try: 139 del context[key] 140 except: 141 pass 132 142 else: 133 143 context[self.loopvars[0]] = item 134 144 for node in self.nodelist_loop: 135 145 nodelist.append(node.render(context)) 136 if unpack:137 # The loop variables were pushed on to the context so pop them138 # off again. This is necessary because the tag lets the length139 # of loopvars differ to the length of each set of items and we140 # don't want to leave any vars from the previous loop on the141 # context.142 context.pop()143 146 context.pop() 144 147 return nodelist.render(context) 145 148 … … 583 586 if bits[in_index] != 'in': 584 587 raise TemplateSyntaxError, "'for' statements should use the format 'for x in y': %s" % token.contents 585 588 586 loopvars = re.s ub(r' *, *', ',', ' '.join(bits[1:in_index])).split(',')589 loopvars = re.split(r' *, *', ' '.join(bits[1:in_index])) 587 590 for var in loopvars: 588 591 if not var or ' ' in var: 589 592 raise TemplateSyntaxError, "'for' tag received an invalid argument: %s" % token.contents