Code

Ticket #546: template_loader.diff

File template_loader.diff, 2.0 KB (added by hugo <gb@…>, 9 years ago)

patch to render_to_string to make use of select_template

Line 
1Index: template_loader.py
2===================================================================
3--- template_loader.py  (revision 671)
4+++ template_loader.py  (working copy)
5@@ -19,29 +19,34 @@
6     """
7     return template.Template(source)
8 
9+def select_template(template_name_list):
10+    "Given a list of template names, returns the first that can be loaded."
11+    for template_name in template_name_list:
12+        try:
13+            return get_template(template_name)
14+        except template.TemplateDoesNotExist:
15+            continue
16+    # If we get here, none of the templates could be loaded
17+    raise template.TemplateDoesNotExist, ', '.join(template_name_list)
18+
19 def render_to_string(template_name, dictionary=None, context_instance=None):
20     """
21     Loads the given template_name and renders it with the given dictionary as
22-    context. Returns a string.
23+    context. Returns a string. If the template_name contains ; it's assumed
24+    that it is a list of different template names and select_template is
25+    used instead of get_template.
26     """
27     dictionary = dictionary or {}
28-    t = get_template(template_name)
29+    if template_name.find(';') > 0:
30+       t = select_template(template_name.split(';'))
31+    else:
32+       t = get_template(template_name)
33     if context_instance:
34         context_instance.update(dictionary)
35     else:
36         context_instance = template.Context(dictionary)
37     return t.render(context_instance)
38 
39-def select_template(template_name_list):
40-    "Given a list of template names, returns the first that can be loaded."
41-    for template_name in template_name_list:
42-        try:
43-            return get_template(template_name)
44-        except template.TemplateDoesNotExist:
45-            continue
46-    # If we get here, none of the templates could be loaded
47-    raise template.TemplateDoesNotExist, ', '.join(template_name_list)
48-
49 class SuperBlock:
50     "This implements the ability for {{ block.super }} to render the parent block's contents"
51     def __init__(self, context, nodelist):