Code

Ticket #5124: must_be_first.diff

File must_be_first.diff, 1.6 KB (added by k0001, 7 years ago)

adds support for a new Node property "must_be_first"

Line 
1Index: django/template/__init__.py
2===================================================================
3--- django/template/__init__.py (revision 6783)
4+++ django/template/__init__.py (working copy)
5@@ -311,6 +311,8 @@
6         return NodeList()
7 
8     def extend_nodelist(self, nodelist, node, token):
9+        if node.must_be_first and len(nodelist):
10+            raise TemplateSyntaxError, "%r must be the first Node on the template." % node
11         nodelist.append(node)
12 
13     def enter_command(self, command, token):
14@@ -780,6 +782,9 @@
15         return current
16 
17 class Node(object):
18+    # Set this to True if the node must be the first on a NodeList.
19+    must_be_first = False
20+
21     def render(self, context):
22         "Return the node rendered as a string"
23         pass
24Index: django/template/loader_tags.py
25===================================================================
26--- django/template/loader_tags.py      (revision 6783)
27+++ django/template/loader_tags.py      (working copy)
28@@ -37,11 +37,16 @@
29             self.parent = BlockNode(self.name, nodelist)
30 
31 class ExtendsNode(Node):
32+    must_be_first = True
33+
34     def __init__(self, nodelist, parent_name, parent_name_expr, template_dirs=None):
35         self.nodelist = nodelist
36         self.parent_name, self.parent_name_expr = parent_name, parent_name_expr
37         self.template_dirs = template_dirs
38 
39+    def __repr__(self):
40+        return '<ExtendsNode: %s>' % self.parent_name
41+
42     def get_parent(self, context):
43         if self.parent_name_expr:
44             self.parent_name = self.parent_name_expr.resolve(context)