Code

Ticket #9874: template_cache.diff

File template_cache.diff, 2.0 KB (added by novoya, 5 years ago)

templace cache patch

Line 
1Index: django1.02/django/template/loader_tags.py
2===================================================================
3--- django1.02/django/template/loader_tags.py   (revision 9660)
4+++ django1.02/django/template/loader_tags.py   (working copy)
5@@ -61,11 +61,9 @@
6         if hasattr(parent, 'render'):
7             return parent # parent is a Template object
8         try:
9-            source, origin = find_template_source(parent, self.template_dirs)
10+            return get_template(parent)
11         except TemplateDoesNotExist:
12-            raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent
13-        else:
14-            return get_template_from_string(source, origin, parent)
15+            raise TemplateSyntaxError, "Template %r cannot be extended, because it doesn't exist" % parent       
16 
17     def render(self, context):
18         compiled_parent = self.get_parent(context)
19Index: django1.02/django/template/loader.py
20===================================================================
21--- django1.02/django/template/loader.py        (revision 9660)
22+++ django1.02/django/template/loader.py        (working copy)
23@@ -25,6 +25,7 @@
24 from django.conf import settings
25 
26 template_source_loaders = None
27+template_cache = {}
28 
29 class LoaderOrigin(Origin):
30     def __init__(self, display_name, loader, name, dirs):
31@@ -77,8 +78,13 @@
32     Returns a compiled Template object for the given template name,
33     handling template inheritance recursively.
34     """
35-    source, origin = find_template_source(template_name)
36-    template = get_template_from_string(source, origin, template_name)
37+    global template_cache
38+    if template_name in template_cache:
39+        template = template_cache[template_name]
40+    else:
41+        source, origin = find_template_source(template_name)
42+        template = get_template_from_string(source, origin, template_name)
43+        template_cache[template_name] = template
44     return template
45 
46 def get_template_from_string(source, origin=None, name=None):