diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2007-03-27 17:25:56 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2007-03-27 17:25:56 +0000 |
| commit | d6fd9fb22bdbc6939fbe97c6e0571b8002d014c6 (patch) | |
| tree | eef5c47d38ed97cbebefc1b1f92a3aa7f0d41b30 /django | |
| parent | 750ea554c12b34f7b68653488f345f4066191e08 (diff) | |
Fixed #3826: added a {% with %}. Thanks, SmileyChris.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4830 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/template/defaulttags.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index ed11f2c1ae..cf72d8f26e 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -354,6 +354,23 @@ class WidthRatioNode(Node): return '' return str(int(round(ratio))) +class WithNode(Node): + def __init__(self, var, name, nodelist): + self.var = var + self.name = name + self.nodelist = nodelist + + def __repr__(self): + return "<WithNode>" + + def render(self, context): + val = self.var.resolve(context) + context.push() + context[self.name] = val + output = self.nodelist.render(context) + context.pop() + return output + #@register.tag def comment(parser, token): """ @@ -967,3 +984,23 @@ def widthratio(parser, token): return WidthRatioNode(parser.compile_filter(this_value_expr), parser.compile_filter(max_value_expr), max_width) widthratio = register.tag(widthratio) + +#@register.tag +def do_with(parser, token): + """ + Add a value to the context (inside of this block) for caching and easy + access. For example:: + + {% with person.some_sql_method as total %} + {{ total }} object{{ total|pluralize }} + {% endwith %} + """ + bits = list(token.split_contents()) + if len(bits) != 4 or bits[2] != "as": + raise TemplateSyntaxError, "%r expected format is 'value as name'" % tagname + var = parser.compile_filter(bits[1]) + name = bits[3] + nodelist = parser.parse(('endwith',)) + parser.delete_first_token() + return WithNode(var, name, nodelist) +do_with = register.tag('with', do_with) |
