Index: django-trunk/django/core/defaulttags.py
===================================================================
--- django-trunk/django/core/defaulttags.py (revision 544)
+++ django-trunk/django/core/defaulttags.py (working copy)
@@ -25,6 +25,20 @@
output.append(pformat(sys.modules))
return ''.join(output)
+class DefineNode(template.Node):
+ def __init__(self, name, value, body):
+ self.name = name
+ self.value = value
+ self.body = body
+
+ def render(self, context):
+ value = self.value.render(context)
+ context.push()
+ context[self.name] = value
+ rendering = self.body.render(context)
+ context.pop()
+ return rendering
+
class FilterNode(template.Node):
def __init__(self, filters, nodelist):
self.filters, self.nodelist = filters, nodelist
@@ -359,6 +373,52 @@
"Print a whole load of debugging information, including the context and imported modules"
return DebugNode()
+def do_define(parser, token, parse_until):
+ """Sets variable until the end of the template or enclosing scope.
+
+ Usage::
+
+ {% define VAR as %}
+ VALUE
+ {% in %}
+ BODY ...
+
+ Example::
+
+ {% define title_var as%}
+ {% block title %}The default title{% endblock %}
+ {% in %}
+
+
+ {{title_var}}
+
+
+ {{title_var}}
+ ...
+
+ In this example, block title is evaluated once and the result is
+ assigned to the variable title_var, which is used twice.
+
+ If the definition is enclosed by a for-, if- or else-block, it lasts
+ ONLY to the END of the enclosing block.
+ """
+ bits = token.contents.split()
+ if len(bits) != 3:
+ raise template.TemplateSyntaxError, "'define' statements should have three words"
+ if bits[2] != 'as':
+ raise template.TemplateSyntaxError, "third word of 'define' statement should be '%s'" % bits[2]
+ name = bits[1]
+ # parse the value
+ value = parser.parse(('in',))
+ parser.delete_first_token()
+ # parse up to the end of the enclosing block
+ body = parser.parse(parse_until)
+ return DefineNode(name, value, body)
+
+
+
+
+
def do_filter(parser, token):
"""
Filter the contents of the blog through variable filters.
@@ -733,6 +793,7 @@
template.register_tag('comment', do_comment)
template.register_tag('cycle', do_cycle)
template.register_tag('debug', do_debug)
+template.register_tag('define', do_define)
template.register_tag('filter', do_filter)
template.register_tag('firstof', do_firstof)
template.register_tag('for', do_for)