Ticket #3523: for_tag.2.patch

File for_tag.2.patch, 3.0 KB (added by Honza Král <Honza.Kral@…>, 18 years ago)

sorry, a minor problem with the previous version

  • django/template/defaulttags.py

     
    5858        return ''
    5959
    6060class ForNode(Node):
    61     def __init__(self, loopvar, sequence, reversed, nodelist_loop):
    62         self.loopvar, self.sequence = loopvar, sequence
     61    def __init__(self, loopvars, sequence, reversed, nodelist_loop):
     62        self.loopvars, self.sequence = loopvars, sequence
    6363        self.reversed = reversed
    6464        self.nodelist_loop = nodelist_loop
    6565
     
    6969        else:
    7070            reversed = ''
    7171        return "<For Node: for %s in %s, tail_len: %d%s>" % \
    72             (self.loopvar, self.sequence, len(self.nodelist_loop), reversed)
     72            (', '.join( self.loopvars ), self.sequence, len(self.nodelist_loop), reversed)
    7373
    7474    def __iter__(self):
    7575        for node in self.nodelist_loop:
     
    117117                'last': (i == len_values - 1),
    118118                'parentloop': parentloop,
    119119            }
    120             context[self.loopvar] = item
     120            context.update( dict( zip(self.loopvars, item) ) )
     121            #context[self.loopvar] = item
    121122            for node in self.nodelist_loop:
    122123                nodelist.append(node.render(context))
    123124        context.pop()
     
    523524
    524525    """
    525526    bits = token.contents.split()
    526     if len(bits) == 5 and bits[4] != 'reversed':
    527         raise TemplateSyntaxError, "'for' statements with five words should end in 'reversed': %s" % token.contents
    528     if len(bits) not in (4, 5):
    529         raise TemplateSyntaxError, "'for' statements should have either four or five words: %s" % token.contents
    530     if bits[2] != 'in':
    531         raise TemplateSyntaxError, "'for' statement must contain 'in' as the second word: %s" % token.contents
    532     loopvar = bits[1]
    533     sequence = parser.compile_filter(bits[3])
    534     reversed = (len(bits) == 5)
     527    #if len(bits) == 5 and bits[4] != 'reversed':
     528    #    raise TemplateSyntaxError, "'for' statements with five words should end in 'reversed': %s" % token.contents
     529    if len(bits) < 4:
     530        raise TemplateSyntaxError, "'for' statements should have at leat four words: %s" % token.contents
     531
     532    try:
     533        in_index = bits.index('in')
     534    except ValueError:
     535        raise TemplateSyntaxError, "'for' statement must contain 'in': %s" % token.contents
     536
     537    loopvars = []
     538    for bit in bits[1:in_index]:
     539        if bit.find(',') == -1:
     540            loopvars.append( bit )
     541        elif len(bit) > 1:
     542            loopvars.extend( b for b in bit.split(',') if b )
     543
     544    sequence = parser.compile_filter(bits[in_index+1])
     545    reversed = ( bits[-1] == 'reversed' )
    535546    nodelist_loop = parser.parse(('endfor',))
    536547    parser.delete_first_token()
    537     return ForNode(loopvar, sequence, reversed, nodelist_loop)
     548    return ForNode(loopvars, sequence, reversed, nodelist_loop)
    538549do_for = register.tag("for", do_for)
    539550
    540551def do_ifequal(parser, token, negate):
Back to Top