Ticket #4552: for_tag_tidy.patch

File for_tag_tidy.patch, 2.3 KB (added by Chris Beaven, 17 years ago)
  • django/template/defaulttags.py

     
    55from django.template import get_library, Library, InvalidTemplateLibrary
    66from django.conf import settings
    77import sys
     8from itertools import izip
    89import re
    910
    1011if not hasattr(__builtins__, 'reversed'):
     
    127128                'parentloop': parentloop,
    128129            }
    129130            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
    132142            else:
    133143                context[self.loopvars[0]] = item
    134144            for node in self.nodelist_loop:
    135145                nodelist.append(node.render(context))
    136             if unpack:
    137                 # The loop variables were pushed on to the context so pop them
    138                 # off again. This is necessary because the tag lets the length
    139                 # of loopvars differ to the length of each set of items and we
    140                 # don't want to leave any vars from the previous loop on the
    141                 # context.
    142                 context.pop()
    143146        context.pop()
    144147        return nodelist.render(context)
    145148
     
    583586    if bits[in_index] != 'in':
    584587        raise TemplateSyntaxError, "'for' statements should use the format 'for x in y': %s" % token.contents
    585588
    586     loopvars = re.sub(r' *, *', ',', ' '.join(bits[1:in_index])).split(',')
     589    loopvars = re.split(r' *, *', ' '.join(bits[1:in_index]))
    587590    for var in loopvars:
    588591        if not var or ' ' in var:
    589592            raise TemplateSyntaxError, "'for' tag received an invalid argument: %s" % token.contents
Back to Top