Ticket #3826: with_tag.patch
File with_tag.patch, 3.4 KB (added by , 18 years ago) |
---|
-
django/template/defaulttags.py
354 354 return '' 355 355 return str(int(round(ratio))) 356 356 357 class WithNode(Node): 358 def __init__(self, var, name, nodelist): 359 self.var = var 360 self.name = name 361 self.nodelist = nodelist 362 363 def __repr__(self): 364 return "<WithNode>" 365 366 def render(self, context): 367 val = self.var.resolve(context) 368 context.push() 369 context[self.name] = val 370 output = self.nodelist.render(context) 371 context.pop() 372 return output 373 357 374 #@register.tag 358 375 def comment(parser, token): 359 376 """ … … 967 984 return WidthRatioNode(parser.compile_filter(this_value_expr), 968 985 parser.compile_filter(max_value_expr), max_width) 969 986 widthratio = register.tag(widthratio) 987 988 #@register.tag 989 def do_with(parser, token): 990 """ 991 Add a value to the context (inside of this block) for caching and easy 992 access. For example:: 993 994 {% with person.some_sql_method as total %} 995 {{ total }} object{{ total|pluralize }} 996 {% endwith %} 997 """ 998 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] 1003 nodelist = parser.parse(('endwith',)) 1004 parser.delete_first_token() 1005 return WithNode(var, name, nodelist) 1006 do_with = register.tag('with', do_with) -
docs/templates.txt
878 878 above example will be 88 pixels wide (because 175/200 = .875; .875 * 100 = 87.5 879 879 which is rounded up to 88). 880 880 881 with 882 ~~~~ 883 884 **New in Django development version** 885 886 Useful for caching a method which will be used more than once. 887 888 For example:: 889 890 {% with person.some_sql_method as total %} 891 {{ total }} person object{{ total|pluralize }} 892 {% endwith %} 893 894 The populated variable (in the example above, ``total``) is only available 895 inside of ``{% with %}`` block. 896 881 897 Built-in filter reference 882 898 ------------------------- 883 899 -
tests/regressiontests/templates/tests.py
650 650 'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError), 651 651 'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, template.TemplateSyntaxError), 652 652 653 ### WITH TAG ######################################################## 654 'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'), 655 'with02': ('{{ key }}{% with dict.key as key %}{{ key }}-{{ dict.key }}-{{ key }}{% endwith %}{{ key }}', {'dict': {'key':50}}, ('50-50-50', 'INVALID50-50-50INVALID')), 656 653 657 ### NOW TAG ######################################################## 654 658 # Simple case 655 659 'now01' : ('{% now "j n Y"%}', {}, str(datetime.now().day) + ' ' + str(datetime.now().month) + ' ' + str(datetime.now().year)),