Ticket #6378: 6378-into.diff
File 6378-into.diff, 3.8 KB (added by , 16 years ago) |
---|
-
django/template/defaulttags.py
416 416 return str(int(round(ratio))) 417 417 418 418 class WithNode(Node): 419 def __init__(self, var, name, nodelist ):419 def __init__(self, var, name, nodelist, into): 420 420 self.var = var 421 421 self.name = name 422 422 self.nodelist = nodelist 423 self.into = into 423 424 424 425 def __repr__(self): 425 426 return "<WithNode>" … … 430 431 context[self.name] = val 431 432 output = self.nodelist.render(context) 432 433 context.pop() 433 return output 434 if not self.into: 435 return output 436 else: 437 context[self.into] = output 438 return '' 434 439 435 440 #@register.tag 436 441 def autoescape(parser, token): … … 1160 1165 {% with person.some_sql_method as total %} 1161 1166 {{ total }} object{{ total|pluralize }} 1162 1167 {% endwith %} 1168 1169 {% with person.some_sql_method as total into content %} 1170 {{ total }} object{{ total|pluralize }} 1171 {% endwith %} 1172 {{ content }} 1173 1163 1174 """ 1164 1175 bits = list(token.split_contents()) 1165 if len(bits) != 4 or bits[2] != "as": 1166 raise TemplateSyntaxError("%r expected format is 'value as name'" % 1176 if len(bits) == 4: 1177 if bits[2] != "as": 1178 raise TemplateSyntaxError("%r expected format is 'value as name [into context]'" % 1167 1179 bits[0]) 1180 elif len(bits) == 6: 1181 if bits[2] != "as" or bits[4] != "into": 1182 raise TemplateSyntaxError("%r expected format is 'value as name [into context]'" % 1183 bits[0]) 1184 else: 1185 raise TemplateSyntaxError("%r expected format is 'value as name [into context]'" % 1186 bits[0]) 1187 1168 1188 var = parser.compile_filter(bits[1]) 1169 1189 name = bits[3] 1170 1190 nodelist = parser.parse(('endwith',)) 1171 1191 parser.delete_first_token() 1172 return WithNode(var, name, nodelist) 1192 if len(bits) == 4: 1193 return WithNode(var, name, nodelist, '') 1194 elif len(bits) == 6: 1195 into = bits[5] 1196 return WithNode(var, name, nodelist, into) 1197 1173 1198 do_with = register.tag('with', do_with) -
tests/regressiontests/templates/tests.py
891 891 ### WITH TAG ######################################################## 892 892 'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'), 893 893 'with02': ('{{ key }}{% with dict.key as key %}{{ key }}-{{ dict.key }}-{{ key }}{% endwith %}{{ key }}', {'dict': {'key':50}}, ('50-50-50', 'INVALID50-50-50INVALID')), 894 'with03': ('{% with dict.key as key into content %}{{ key }}{% endwith %}{{ content }}', {'dict': {'key': 50}}, '50'), 895 'with04': ('{{ content }}-{% with dict.key as key into content %}{{ key }}{% endwith %}-{{ content }}', {'dict': {'key': 50}}, '--50'), 894 896 895 897 'with-error01': ('{% with dict.key xx key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError), 896 898 'with-error02': ('{% with dict.key as %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError), 899 'with-error03': ('{% with dict.key as key into %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError), 900 'with-error04': ('{% with dict.key as key blah content %}{{ key }}{% endwith %}', {'dict': {'key':50}}, template.TemplateSyntaxError), 897 901 898 902 ### NOW TAG ######################################################## 899 903 # Simple case