26
26
# installed, because pkg_resources is necessary to read eggs.
28
28
from django.core.exceptions import ImproperlyConfigured
29
from django.template.base import Origin, Template, Context, TemplateDoesNotExist, add_to_builtins
29
from django.template.base import Origin, Template, Context, TemplateDoesNotExist
30
30
from django.conf import settings
31
from django.utils.module_loading import import_by_path
31
from django.utils.module_loading import import_string
32
32
from django.utils import six
34
34
template_source_loaders = None
36
37
class BaseLoader(object):
80
82
return self.loader(self.loadname, self.dirs)[0]
82
85
def make_origin(display_name, loader, name, dirs):
83
86
if settings.TEMPLATE_DEBUG and display_name:
84
87
return LoaderOrigin(display_name, loader, name, dirs)
88
92
def find_template_loader(loader):
89
93
if isinstance(loader, (tuple, list)):
90
94
loader, args = loader[0], loader[1:]
93
97
if isinstance(loader, six.string_types):
94
TemplateLoader = import_by_path(loader)
98
TemplateLoader = import_string(loader)
96
100
if hasattr(TemplateLoader, 'load_template_source'):
97
101
func = TemplateLoader(*args)
131
136
raise TemplateDoesNotExist(name)
133
def get_template(template_name):
139
def get_template(template_name, dirs=None):
135
141
Returns a compiled Template object for the given template name,
136
142
handling template inheritance recursively.
138
template, origin = find_template(template_name)
144
template, origin = find_template(template_name, dirs)
139
145
if not hasattr(template, 'render'):
140
146
# template needs to be compiled
141
147
template = get_template_from_string(template, origin, template_name)
144
151
def get_template_from_string(source, origin=None, name=None):
146
153
Returns a compiled Template object for the given template code,
149
156
return Template(source, origin, name)
151
def render_to_string(template_name, dictionary=None, context_instance=None):
159
def render_to_string(template_name, dictionary=None, context_instance=None,
153
162
Loads the given template_name and renders it with the given dictionary as
154
163
context. The template_name may be a string to load a single template using
155
164
get_template, or it may be a tuple to use select_template to find one of
156
165
the templates in the list. Returns a string.
158
dictionary = dictionary or {}
159
167
if isinstance(template_name, (list, tuple)):
160
t = select_template(template_name)
168
t = select_template(template_name, dirs)
162
t = get_template(template_name)
170
t = get_template(template_name, dirs)
163
171
if not context_instance:
164
172
return t.render(Context(dictionary))
174
return t.render(context_instance)
165
175
# Add the dictionary to the context stack, ensuring it gets removed again
166
176
# to keep the context_instance in the same state it started in.
167
context_instance.update(dictionary)
177
with context_instance.push(dictionary):
169
178
return t.render(context_instance)
171
context_instance.pop()
173
def select_template(template_name_list):
181
def select_template(template_name_list, dirs=None):
174
182
"Given a list of template names, returns the first that can be loaded."
175
183
if not template_name_list:
176
184
raise TemplateDoesNotExist("No template names provided")
178
186
for template_name in template_name_list:
180
return get_template(template_name)
188
return get_template(template_name, dirs)
181
189
except TemplateDoesNotExist as e:
182
190
if e.args[0] not in not_found:
183
191
not_found.append(e.args[0])
185
193
# If we get here, none of the templates could be loaded
186
194
raise TemplateDoesNotExist(', '.join(not_found))
188
add_to_builtins('django.template.loader_tags')