Changeset 4830
- Timestamp:
- 03/27/07 12:25:56 (1 year ago)
- Files:
-
- django/trunk/django/template/defaulttags.py (modified) (2 diffs)
- django/trunk/docs/templates.txt (modified) (1 diff)
- django/trunk/tests/regressiontests/templates/tests.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/template/defaulttags.py
r4816 r4830 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): … … 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) django/trunk/docs/templates.txt
r4804 r4830 873 873 which is rounded up to 88). 874 874 875 with 876 ~~~~ 877 878 **New in Django development version** 879 880 Useful for caching a method which will be used more than once. 881 882 For example:: 883 884 {% with person.some_sql_method as total %} 885 {{ total }} person object{{ total|pluralize }} 886 {% endwith %} 887 888 The populated variable (in the example above, ``total``) is only available 889 inside of ``{% with %}`` block. 890 875 891 Built-in filter reference 876 892 ------------------------- django/trunk/tests/regressiontests/templates/tests.py
r4690 r4830 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
