| | 408 | class 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 | |
| | 1123 | |
| | 1124 | def 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) |
| | 1144 | do_withblock = register.tag("withblock", do_withblock) |
| | 1145 | No newline at end of file |