Ticket #8297: 8297.withblock.diff

File 8297.withblock.diff, 2.0 KB (added by Julien Phalip, 16 years ago)
  • django/django/template/defaulttags.py

     
    405405        context.pop()
    406406        return output
    407407
     408class WithBlockNode(Node):
     409    def __init__(self, with_nodelists, in_nodelist):
     410        self.with_nodelists, self.in_nodelist = with_nodelists, in_nodelist
     411
     412    def __repr__(self):
     413        return "<WithBlock node>"
     414
     415    def render(self, context):
     416        in_context = Context()
     417        for (var_name, nodelist) in self.with_nodelists:
     418            in_context[var_name] = nodelist.render(context)
     419        in_context.update(context)
     420        return self.in_nodelist.render(in_context)
     421
    408422#@register.tag
    409423def autoescape(parser, token):
    410424    """
     
    11061120    parser.delete_first_token()
    11071121    return WithNode(var, name, nodelist)
    11081122do_with = register.tag('with', do_with)
     1123
     1124def do_withblock(parser, token):
     1125    bits = token.contents.split()
     1126    if not (len(bits) == 3 and bits[1] == 'as'):
     1127        raise TemplateSyntaxError("'withblock' statement requires 2 arguments: `as` followed by a variable name")
     1128    with_nodelists = []
     1129    var_name = bits[2]
     1130    while True:
     1131        nodelist = parser.parse(('and', 'in'))
     1132        with_nodelists.append((var_name, nodelist))
     1133        token = parser.next_token()
     1134        if token.contents == 'in':
     1135            break
     1136        else:
     1137            bits = token.contents.split()
     1138            if not (len(bits) == 3 and bits[1] == 'as'):
     1139                raise TemplateSyntaxError("'and' statement requires 2 arguments: `as` followed by a variable name")
     1140            var_name = bits[2]
     1141    in_nodelist = parser.parse(('endwithblock'))
     1142    parser.delete_first_token()
     1143    return WithBlockNode(with_nodelists, in_nodelist)
     1144do_withblock = register.tag("withblock", do_withblock)
     1145 No newline at end of file
Back to Top