Code

Ticket #3544: 3544.contexttemplatecache.diff

File 3544.contexttemplatecache.diff, 2.6 KB (added by emulbreh, 6 years ago)
Line 
1Index: django/template/context.py
2===================================================================
3--- django/template/context.py  (revision 7573)
4+++ django/template/context.py  (working copy)
5@@ -13,6 +13,7 @@
6         dict_ = dict_ or {}
7         self.dicts = [dict_]
8         self.autoescape = autoescape
9+        self.template_cache = {}
10 
11     def __repr__(self):
12         return repr(self.dicts)
13@@ -64,6 +65,12 @@
14         "Like dict.update(). Pushes an entire dictionary's keys and values onto the context."
15         self.dicts = [other_dict] + self.dicts
16         return other_dict
17+       
18+    def get_template(self, template_name):
19+        if not template_name in self.template_cache:
20+            from django.template.loader import get_template
21+            self.template_cache[template_name] = get_template(template_name)
22+        return self.template_cache[template_name]
23 
24 # This is a function rather than module-level procedural code because we only
25 # want it to execute if somebody uses RequestContext.
26Index: django/template/loader_tags.py
27===================================================================
28--- django/template/loader_tags.py      (revision 7573)
29+++ django/template/loader_tags.py      (working copy)
30@@ -92,22 +92,6 @@
31                 parent_block.nodelist = block_node.nodelist
32         return compiled_parent.render(context)
33 
34-class ConstantIncludeNode(Node):
35-    def __init__(self, template_path):
36-        try:
37-            t = get_template(template_path)
38-            self.template = t
39-        except:
40-            if settings.TEMPLATE_DEBUG:
41-                raise
42-            self.template = None
43-
44-    def render(self, context):
45-        if self.template:
46-            return self.template.render(context)
47-        else:
48-            return ''
49-
50 class IncludeNode(Node):
51     def __init__(self, template_name):
52         self.template_name = Variable(template_name)
53@@ -115,7 +99,7 @@
54     def render(self, context):
55         try:
56             template_name = self.template_name.resolve(context)
57-            t = get_template(template_name)
58+            t = context.get_template(template_name)
59             return t.render(context)
60         except TemplateSyntaxError, e:
61             if settings.TEMPLATE_DEBUG:
62@@ -178,9 +162,6 @@
63     bits = token.contents.split()
64     if len(bits) != 2:
65         raise TemplateSyntaxError, "%r tag takes one argument: the name of the template to be included" % bits[0]
66-    path = bits[1]
67-    if path[0] in ('"', "'") and path[-1] == path[0]:
68-        return ConstantIncludeNode(path[1:-1])
69     return IncludeNode(bits[1])
70 
71 register.tag('block', do_block)