Code

Ticket #398: define-tag.diff

File define-tag.diff, 2.7 KB (added by jvr_django@…, 9 years ago)

patch for new {%define VAR as%}VALUE{%in%} tag

Line 
1Index: django-trunk/django/core/defaulttags.py
2===================================================================
3--- django-trunk/django/core/defaulttags.py     (revision 544)
4+++ django-trunk/django/core/defaulttags.py     (working copy)
5@@ -25,6 +25,20 @@
6         output.append(pformat(sys.modules))
7         return ''.join(output)
8 
9+class DefineNode(template.Node):
10+    def __init__(self, name, value, body):
11+        self.name = name
12+        self.value = value
13+        self.body = body
14+
15+    def render(self, context):
16+        value = self.value.render(context)
17+        context.push()
18+        context[self.name] = value
19+        rendering =  self.body.render(context)
20+        context.pop()
21+        return rendering
22+
23 class FilterNode(template.Node):
24     def __init__(self, filters, nodelist):
25         self.filters, self.nodelist = filters, nodelist
26@@ -359,6 +373,52 @@
27     "Print a whole load of debugging information, including the context and imported modules"
28     return DebugNode()
29 
30+def do_define(parser, token, parse_until):
31+    """Sets variable until the end of the template or enclosing scope.
32+
33+    Usage::
34+
35+        {% define VAR as %}
36+        VALUE
37+        {% in %}
38+        BODY ...
39+
40+    Example::
41+
42+        {% define title_var as%}
43+            {% block title %}The default title{% endblock %}
44+        {% in %}
45+        <html>
46+        <head>
47+            <title>{{title_var}}</title>
48+        </head>
49+        <body>
50+            <h1>{{title_var}}</h1>
51+            ...
52+
53+    In this example, block title is evaluated once and the result is
54+    assigned to the variable title_var, which is used twice.
55+
56+    If the definition is enclosed by a for-, if- or else-block, it lasts
57+    ONLY to the END of the enclosing block.
58+    """
59+    bits = token.contents.split()
60+    if len(bits) != 3:
61+        raise template.TemplateSyntaxError, "'define' statements should have three words"
62+    if bits[2] != 'as':
63+        raise template.TemplateSyntaxError, "third word of 'define' statement should be '%s'" % bits[2]
64+    name = bits[1]
65+    # parse the value
66+    value = parser.parse(('in',))
67+    parser.delete_first_token()
68+    # parse up to the end of the enclosing block
69+    body = parser.parse(parse_until)
70+    return DefineNode(name, value, body)
71+
72+
73+
74+
75+
76 def do_filter(parser, token):
77     """
78     Filter the contents of the blog through variable filters.
79@@ -733,6 +793,7 @@
80 template.register_tag('comment', do_comment)
81 template.register_tag('cycle', do_cycle)
82 template.register_tag('debug', do_debug)
83+template.register_tag('define', do_define)
84 template.register_tag('filter', do_filter)
85 template.register_tag('firstof', do_firstof)
86 template.register_tag('for', do_for)
87