Ticket #9456: with.patch
File with.patch, 3.4 KB (added by , 16 years ago) |
---|
-
django/template/defaulttags.py
407 407 return str(int(round(ratio))) 408 408 409 409 class WithNode(Node): 410 def __init__(self, var , name, nodelist):411 self.var = var412 self.name = name410 def __init__(self, varlist, namelist, nodelist): 411 self.varlist = varlist 412 self.namelist = namelist 413 413 self.nodelist = nodelist 414 414 415 415 def __repr__(self): 416 416 return "<WithNode>" 417 417 418 418 def render(self, context): 419 val = self.var.resolve(context)420 419 context.push() 421 context[self.name] = val 420 for name, var in zip(self.namelist, self.varlist): 421 context[name] = var.resolve(context) 422 422 output = self.nodelist.render(context) 423 423 context.pop() 424 424 return output … … 1115 1115 def do_with(parser, token): 1116 1116 """ 1117 1117 Adds a value to the context (inside of this block) for caching and easy 1118 access. 1118 access. You bind a value to a new name using ``as``. You can chain multiple 1119 bindings with ``and`` which are evaluated sequentially. 1119 1120 1120 1121 For example:: 1121 1122 1122 1123 {% with person.some_sql_method as total %} 1123 1124 {{ total }} object{{ total|pluralize }} 1124 1125 {% endwith %} 1126 1127 {% with value1 as var1 and value2 as var2 %} 1128 {{ var1 }} ... {{ var2 }} 1129 {% endwith %} 1125 1130 """ 1126 1131 bits = list(token.split_contents()) 1127 if len(bits) != 4 or bits[2] != "as":1128 raise TemplateSyntaxError("%r expected format is 'value as name '" %1132 if len(bits) % 4 or any(b != "as" for b in bits[2::4]): 1133 raise TemplateSyntaxError("%r expected format is 'value as name {and value as name}...'" % 1129 1134 bits[0]) 1130 var = parser.compile_filter(bits[1])1131 name = bits[3]1135 varlist = [parser.compile_filter(b) for b in bits[1::4]] 1136 namelist = bits[3::4] 1132 1137 nodelist = parser.parse(('endwith',)) 1133 1138 parser.delete_first_token() 1134 return WithNode(var , name, nodelist)1139 return WithNode(varlist, namelist, nodelist) 1135 1140 do_with = register.tag('with', do_with) -
tests/regressiontests/templates/tests.py
888 888 ### WITH TAG ######################################################## 889 889 'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'), 890 890 'with02': ('{{ key }}{% with dict.key as key %}{{ key }}-{{ dict.key }}-{{ key }}{% endwith %}{{ key }}', {'dict': {'key':50}}, ('50-50-50', 'INVALID50-50-50INVALID')), 891 'with03': ('{% with dict.k1 as k1 and dict.k2 as k2 %}{{k1}}+{{k2}}{% endwith %}', {'dict': {'k1':3, 'k2':4}}, '3+4'), 891 892 892 893 'with-error01': ('{% with dict.key xx key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError), 893 894 'with-error02': ('{% with dict.key as %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError), 895 'with-error03': ('{% with dict.k1 as k1 and dict.k2 %}{{ key }}{% endwith %}', {'dict': {'k1':3, 'k2':4}}, template.TemplateSyntaxError), 894 896 895 897 ### NOW TAG ######################################################## 896 898 # Simple case