Ticket #3826: with_template_tag.patch
File with_template_tag.patch, 4.0 KB (added by , 18 years ago) |
---|
-
trunk/django/template/defaulttags.py
355 355 return str(int(round(ratio))) 356 356 357 357 class WithNode(Node): 358 def __init__(self, var, name, nodelist): 359 self.var = var 360 self.name = name 358 def __init__(self, varlist, nodelist): 359 self.varlist = varlist 361 360 self.nodelist = nodelist 362 361 363 362 def __repr__(self): 364 363 return "<WithNode>" 365 364 366 365 def render(self, context): 367 val = self.var.resolve(context)368 context.push()369 context[self.name] = val366 context.push() # start a new context 367 for var, name in self.varlist: 368 context[name] = var.resolve(context) 370 369 output = self.nodelist.render(context) 371 context.pop() 370 context.pop() # delete the new context 372 371 return output 373 372 374 373 #@register.tag … … 988 987 #@register.tag 989 988 def do_with(parser, token): 990 989 """ 991 Add a valueto the context (inside of this block) for caching and easy990 Add values to the context (inside of this block) for caching and easy 992 991 access. For example:: 993 992 994 993 {% with person.some_sql_method as total %} 995 994 {{ total }} object{{ total|pluralize }} 996 995 {% endwith %} 996 {% with person.some_sql_method as total and person.name as name %} 997 {{ name }}: {{ total }} object{{ total| pluralize }} 998 {% endwith %} 997 999 """ 998 1000 bits = list(token.split_contents()) 999 if len(bits) != 4 or bits[2] != "as": 1000 raise TemplateSyntaxError, "%r expected format is 'value as name'" % tagname 1001 var = parser.compile_filter(bits[1]) 1002 name = bits[3] 1001 bits.pop(0) 1002 if not bits: 1003 raise TemplateSyntaxError, "'with' expected format is 'value as name' or 'value1 as name1 and value2 as name2...'" 1004 bitlist = [bitstr.split(' ') for bitstr in ' '.join(bits).split(' and ')] 1005 try: 1006 varlist = [(parser.compile_filter(var), name) for var, prep, name in bitlist] 1007 except ValueError: 1008 raise TemplateSyntaxError, "'with' expected format is 'value as name' or 'value1 as name1 and value2 as name2...'" 1003 1009 nodelist = parser.parse(('endwith',)) 1004 1010 parser.delete_first_token() 1005 return WithNode(var , name, nodelist)1011 return WithNode(varlist, nodelist) 1006 1012 do_with = register.tag('with', do_with) -
trunk/tests/regressiontests/templates/tests.py
653 653 ### WITH TAG ######################################################## 654 654 'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'), 655 655 'with02': ('{{ key }}{% with dict.key as key %}{{ key }}-{{ dict.key }}-{{ key }}{% endwith %}{{ key }}', {'dict': {'key':50}}, ('50-50-50', 'INVALID50-50-50INVALID')), 656 'with03': ('{{ key1 }}{{ key2 }}{% with dict.key1 as key1 and dict.key2 as key2 %}{{ key1 }}{{ key 2 }}-{{ dict.key1 }}{{ dict.key2 }}-{{ key1 }}{{ key2 }}{% endwith %}{{ key1 }}{{ key2 }}', {'dict': {'key1':50, 'key2':60}}, ('5060-5060-5060', 'INVALIDINVALID5060-5060-5060INVALIDINVALID')), 656 657 657 658 ### NOW TAG ######################################################## 658 659 # Simple case -
trunk/docs/templates.txt
887 887 {{ total }} employee{{ total|pluralize }} 888 888 {% endwith %} 889 889 890 Or even:: 891 892 {% with business.name as name and business.employees.count as total %} 893 {{ name }}: employee{{ total|pluralize }} 894 {% endwith %} 895 890 896 The populated variable (in the example above, ``total``) is only available 891 897 between the ``{% with %}`` and ``{% endwith %}`` tags. 892 898