Code

Ticket #598: django-include-tag.diff

File django-include-tag.diff, 3.1 KB (added by robert@…, 9 years ago)

The patch

Line 
1Index: defaulttags.py
2===================================================================
3--- defaulttags.py      (.../trunk/django/core/defaulttags.py)  (revision 803)
4+++ defaulttags.py      (.../branches/new-admin/django/core/defaulttags.py)     (revision 803)
5@@ -2,6 +2,7 @@
6 
7 import sys
8 import template
9+import template_loader
10 
11 class CommentNode(template.Node):
12     def render(self, context):
13@@ -226,6 +227,35 @@
14                 return '' # Fail silently for invalid included templates.
15         return output
16 
17+
18+class ConstantIncludeNode(template.Node):
19+    def __init__(self, template_path):
20+        try:
21+            t = template_loader.get_template(template_path)       
22+            self.nodelist = t.nodelist
23+        except Exception, e:
24+            self.nodelist = None
25+
26+    def render(self, context):
27+        if self.nodelist:
28+            return self.nodelist.render(context)
29+        else:
30+            return ''
31+
32+class IncludeNode(template.Node):
33+    def __init__(self, template_path_var):
34+        self.template_path_var = template_path_var
35+
36+    def render(self, context):
37+         try:
38+             template_path = template.resolve_variable(self.template_path_var, context)
39+             print "IncludeNode rendering %s" % template_path
40+             t = template_loader.get_template(template_path)
41+             return t.render(context)
42+         except Exception, e:
43+             return '' # Fail silently for invalid included templates.
44+
45+
46 class LoadNode(template.Node):
47     def __init__(self, taglib):
48         self.taglib = taglib
49@@ -607,6 +637,20 @@
50             raise template.TemplateSyntaxError, "Second (optional) argument to %s tag must be 'parsed'" % bits[0]
51     return SsiNode(bits[1], parsed)
52 
53+def do_include(parser, token):
54+    """
55+    Loads a template using standard resolution mechanisms, and renders it in the current context.     
56+    """
57+    bits = token.contents.split()
58+    parsed = False
59+    if len(bits) != 2:
60+        raise template.TemplateSyntaxError, "'include' tag takes one argument: the path to the template to be included"
61+   
62+    path = bits[1]
63+    if path[0] in ('"', "'") and path[-1] == path[0]:
64+        return ConstantIncludeNode(path[1:-1])
65+    return IncludeNode(bits[1])
66+
67 def do_load(parser, token):
68     """
69     Load a custom template tag set.
70@@ -622,8 +666,8 @@
71     # check at compile time that the module can be imported
72     try:
73         LoadNode.load_taglib(taglib)
74-    except ImportError:
75-        raise template.TemplateSyntaxError, "'%s' is not a valid tag library" % taglib
76+    except ImportError, e:
77+        raise template.TemplateSyntaxError, "'%s' is not a valid tag library, %s" % (taglib, e)
78     return LoadNode(taglib)
79 
80 def do_now(parser, token):
81@@ -762,6 +806,7 @@
82 template.register_tag('ifnotequal', lambda parser, token: do_ifequal(parser, token, True))
83 template.register_tag('if', do_if)
84 template.register_tag('ifchanged', do_ifchanged)
85+template.register_tag('include', do_include)
86 template.register_tag('regroup', do_regroup)
87 template.register_tag('ssi', do_ssi)
88 template.register_tag('load', do_load)