2014-11-16 01:35:02 +08:00
|
|
|
import warnings
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
2014-11-16 03:58:26 +08:00
|
|
|
from django.template.base import Origin, Template, Context, TemplateDoesNotExist
|
|
|
|
from django.template.loaders.utils import get_template_loaders
|
2014-11-16 01:35:02 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2005-10-15 06:22:12 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2005-11-24 07:10:17 +08:00
|
|
|
class LoaderOrigin(Origin):
|
|
|
|
def __init__(self, display_name, loader, name, dirs):
|
|
|
|
super(LoaderOrigin, self).__init__(display_name)
|
|
|
|
self.loader, self.loadname, self.dirs = loader, name, dirs
|
|
|
|
|
|
|
|
def reload(self):
|
|
|
|
return self.loader(self.loadname, self.dirs)[0]
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2005-11-24 07:10:17 +08:00
|
|
|
def make_origin(display_name, loader, name, dirs):
|
2010-03-02 07:05:35 +08:00
|
|
|
if settings.TEMPLATE_DEBUG and display_name:
|
2005-11-24 07:10:17 +08:00
|
|
|
return LoaderOrigin(display_name, loader, name, dirs)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2009-12-14 20:08:23 +08:00
|
|
|
def find_template(name, dirs=None):
|
2014-11-16 03:58:26 +08:00
|
|
|
for loader in get_template_loaders():
|
2005-10-15 06:22:12 +08:00
|
|
|
try:
|
2005-11-25 05:14:42 +08:00
|
|
|
source, display_name = loader(name, dirs)
|
2005-11-24 07:10:17 +08:00
|
|
|
return (source, make_origin(display_name, loader, name, dirs))
|
2005-10-15 06:29:13 +08:00
|
|
|
except TemplateDoesNotExist:
|
2005-10-15 06:22:12 +08:00
|
|
|
pass
|
2010-01-11 02:36:20 +08:00
|
|
|
raise TemplateDoesNotExist(name)
|
2005-10-15 04:10:13 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2013-09-16 05:51:24 +08:00
|
|
|
def get_template(template_name, dirs=None):
|
2005-10-15 04:10:13 +08:00
|
|
|
"""
|
|
|
|
Returns a compiled Template object for the given template name,
|
|
|
|
handling template inheritance recursively.
|
|
|
|
"""
|
2013-09-16 05:51:24 +08:00
|
|
|
template, origin = find_template(template_name, dirs)
|
2009-12-14 20:08:23 +08:00
|
|
|
if not hasattr(template, 'render'):
|
|
|
|
# template needs to be compiled
|
|
|
|
template = get_template_from_string(template, origin, template_name)
|
2006-09-02 17:26:24 +08:00
|
|
|
return template
|
2005-10-15 04:10:13 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2006-09-02 17:26:24 +08:00
|
|
|
def get_template_from_string(source, origin=None, name=None):
|
2005-10-15 04:10:13 +08:00
|
|
|
"""
|
|
|
|
Returns a compiled Template object for the given template code,
|
|
|
|
handling template inheritance recursively.
|
|
|
|
"""
|
2006-09-02 17:26:24 +08:00
|
|
|
return Template(source, origin, name)
|
2005-10-15 04:10:13 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2013-09-16 05:51:24 +08:00
|
|
|
def render_to_string(template_name, dictionary=None, context_instance=None,
|
|
|
|
dirs=None):
|
2005-10-15 04:10:13 +08:00
|
|
|
"""
|
2007-06-22 15:15:04 +08:00
|
|
|
Loads the given template_name and renders it with the given dictionary as
|
|
|
|
context. The template_name may be a string to load a single template using
|
|
|
|
get_template, or it may be a tuple to use select_template to find one of
|
|
|
|
the templates in the list. Returns a string.
|
2005-10-15 04:10:13 +08:00
|
|
|
"""
|
|
|
|
if isinstance(template_name, (list, tuple)):
|
2013-09-16 05:51:24 +08:00
|
|
|
t = select_template(template_name, dirs)
|
2005-10-15 04:10:13 +08:00
|
|
|
else:
|
2013-09-16 05:51:24 +08:00
|
|
|
t = get_template(template_name, dirs)
|
2011-02-20 12:55:11 +08:00
|
|
|
if not context_instance:
|
2014-11-22 16:22:38 +08:00
|
|
|
# Django < 1.8 accepted a Context in `dictionary` even though that's
|
|
|
|
# unintended. Preserve this ability but don't rewrap `dictionary`.
|
|
|
|
if isinstance(dictionary, Context):
|
|
|
|
return t.render(dictionary)
|
|
|
|
else:
|
|
|
|
return t.render(Context(dictionary))
|
2014-02-23 05:28:27 +08:00
|
|
|
if not dictionary:
|
|
|
|
return t.render(context_instance)
|
2011-02-20 12:55:11 +08:00
|
|
|
# Add the dictionary to the context stack, ensuring it gets removed again
|
|
|
|
# to keep the context_instance in the same state it started in.
|
2013-07-16 19:11:32 +08:00
|
|
|
with context_instance.push(dictionary):
|
2011-02-20 12:55:11 +08:00
|
|
|
return t.render(context_instance)
|
2005-10-15 04:10:13 +08:00
|
|
|
|
2013-11-03 08:37:15 +08:00
|
|
|
|
2013-09-16 05:51:24 +08:00
|
|
|
def select_template(template_name_list, dirs=None):
|
2005-10-15 04:10:13 +08:00
|
|
|
"Given a list of template names, returns the first that can be loaded."
|
2011-09-21 22:20:18 +08:00
|
|
|
if not template_name_list:
|
|
|
|
raise TemplateDoesNotExist("No template names provided")
|
2011-03-03 08:41:40 +08:00
|
|
|
not_found = []
|
2005-10-15 04:10:13 +08:00
|
|
|
for template_name in template_name_list:
|
|
|
|
try:
|
2013-09-16 05:51:24 +08:00
|
|
|
return get_template(template_name, dirs)
|
2012-04-29 00:09:37 +08:00
|
|
|
except TemplateDoesNotExist as e:
|
2011-03-03 08:41:40 +08:00
|
|
|
if e.args[0] not in not_found:
|
|
|
|
not_found.append(e.args[0])
|
2005-10-15 04:10:13 +08:00
|
|
|
continue
|
|
|
|
# If we get here, none of the templates could be loaded
|
2011-03-03 08:41:40 +08:00
|
|
|
raise TemplateDoesNotExist(', '.join(not_found))
|
2014-11-16 01:35:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
# This line must remain at the bottom to avoid import loops.
|
|
|
|
from .loaders import base
|
|
|
|
|
|
|
|
|
|
|
|
class BaseLoader(base.Loader):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
warnings.warn(
|
|
|
|
"django.template.loader.BaseLoader was renamed to "
|
|
|
|
"django.template.loaders.base.Loader.",
|
|
|
|
RemovedInDjango20Warning, stacklevel=2)
|
|
|
|
super(BaseLoader, self).__init__(*args, **kwargs)
|