2014-11-14 22:48:27 +08:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.utils import lru_cache, six
|
2015-06-23 01:54:35 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango110Warning
|
2014-11-14 22:48:27 +08:00
|
|
|
from django.utils.functional import cached_property
|
2014-11-20 06:23:58 +08:00
|
|
|
from django.utils.module_loading import import_string
|
2014-11-14 22:48:27 +08:00
|
|
|
|
2015-04-25 03:35:30 +08:00
|
|
|
from .base import Context, Template
|
2014-11-21 05:34:59 +08:00
|
|
|
from .context import _builtin_context_processors
|
2015-04-25 03:35:30 +08:00
|
|
|
from .exceptions import TemplateDoesNotExist
|
2015-05-09 04:10:36 +08:00
|
|
|
from .library import import_library
|
2014-11-14 22:48:27 +08:00
|
|
|
|
2014-11-29 06:50:34 +08:00
|
|
|
_context_instance_undefined = object()
|
|
|
|
_dictionary_undefined = object()
|
2014-11-14 22:48:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Engine(object):
|
2015-05-09 04:10:36 +08:00
|
|
|
default_builtins = [
|
|
|
|
'django.template.defaulttags',
|
|
|
|
'django.template.defaultfilters',
|
|
|
|
'django.template.loader_tags',
|
|
|
|
]
|
2014-11-14 22:48:27 +08:00
|
|
|
|
2015-08-17 21:34:50 +08:00
|
|
|
def __init__(self, dirs=None, app_dirs=False, context_processors=None,
|
2014-11-23 04:33:40 +08:00
|
|
|
debug=False, loaders=None, string_if_invalid='',
|
2015-05-09 04:10:36 +08:00
|
|
|
file_charset='utf-8', libraries=None, builtins=None):
|
2014-11-14 22:48:27 +08:00
|
|
|
if dirs is None:
|
|
|
|
dirs = []
|
|
|
|
if context_processors is None:
|
|
|
|
context_processors = []
|
|
|
|
if loaders is None:
|
|
|
|
loaders = ['django.template.loaders.filesystem.Loader']
|
|
|
|
if app_dirs:
|
|
|
|
loaders += ['django.template.loaders.app_directories.Loader']
|
|
|
|
else:
|
|
|
|
if app_dirs:
|
|
|
|
raise ImproperlyConfigured(
|
2014-12-15 06:18:38 +08:00
|
|
|
"app_dirs must not be set when loaders is defined.")
|
2015-05-09 04:10:36 +08:00
|
|
|
if libraries is None:
|
|
|
|
libraries = {}
|
|
|
|
if builtins is None:
|
|
|
|
builtins = []
|
2014-12-15 06:18:38 +08:00
|
|
|
|
2014-11-14 22:48:27 +08:00
|
|
|
self.dirs = dirs
|
|
|
|
self.app_dirs = app_dirs
|
|
|
|
self.context_processors = context_processors
|
2014-11-23 04:33:40 +08:00
|
|
|
self.debug = debug
|
2014-11-14 22:48:27 +08:00
|
|
|
self.loaders = loaders
|
|
|
|
self.string_if_invalid = string_if_invalid
|
2014-11-21 04:27:56 +08:00
|
|
|
self.file_charset = file_charset
|
2015-05-09 04:10:36 +08:00
|
|
|
self.libraries = libraries
|
|
|
|
self.template_libraries = self.get_template_libraries(libraries)
|
|
|
|
self.builtins = self.default_builtins + builtins
|
|
|
|
self.template_builtins = self.get_template_builtins(self.builtins)
|
2014-11-14 22:48:27 +08:00
|
|
|
|
2014-11-28 04:30:35 +08:00
|
|
|
@staticmethod
|
2014-11-14 22:48:27 +08:00
|
|
|
@lru_cache.lru_cache()
|
2014-11-28 04:30:35 +08:00
|
|
|
def get_default():
|
|
|
|
"""
|
|
|
|
When only one DjangoTemplates backend is configured, returns it.
|
|
|
|
|
|
|
|
Raises ImproperlyConfigured otherwise.
|
|
|
|
|
|
|
|
This is required for preserving historical APIs that rely on a
|
|
|
|
globally available, implicitly configured engine such as:
|
|
|
|
|
|
|
|
>>> from django.template import Context, Template
|
|
|
|
>>> template = Template("Hello {{ name }}!")
|
|
|
|
>>> context = Context({'name': "world"})
|
|
|
|
>>> template.render(context)
|
|
|
|
'Hello world!'
|
|
|
|
"""
|
2015-01-04 05:50:20 +08:00
|
|
|
# Since Engine is imported in django.template and since
|
|
|
|
# DjangoTemplates is a wrapper around this Engine class,
|
|
|
|
# local imports are required to avoid import loops.
|
|
|
|
from django.template import engines
|
2014-11-28 04:30:35 +08:00
|
|
|
from django.template.backends.django import DjangoTemplates
|
|
|
|
django_engines = [engine for engine in engines.all()
|
|
|
|
if isinstance(engine, DjangoTemplates)]
|
|
|
|
if len(django_engines) == 1:
|
|
|
|
# Unwrap the Engine instance inside DjangoTemplates
|
|
|
|
return django_engines[0].engine
|
|
|
|
elif len(django_engines) == 0:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"No DjangoTemplates backend is configured.")
|
|
|
|
else:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Several DjangoTemplates backends are configured. "
|
|
|
|
"You must select one explicitly.")
|
2014-11-14 22:48:27 +08:00
|
|
|
|
2014-11-21 05:34:59 +08:00
|
|
|
@cached_property
|
|
|
|
def template_context_processors(self):
|
|
|
|
context_processors = _builtin_context_processors
|
|
|
|
context_processors += tuple(self.context_processors)
|
|
|
|
return tuple(import_string(path) for path in context_processors)
|
|
|
|
|
2015-05-09 04:10:36 +08:00
|
|
|
def get_template_builtins(self, builtins):
|
|
|
|
return [import_library(x) for x in builtins]
|
|
|
|
|
|
|
|
def get_template_libraries(self, libraries):
|
|
|
|
loaded = {}
|
|
|
|
for name, path in libraries.items():
|
|
|
|
loaded[name] = import_library(path)
|
|
|
|
return loaded
|
|
|
|
|
2014-11-14 22:48:27 +08:00
|
|
|
@cached_property
|
|
|
|
def template_loaders(self):
|
2014-11-20 06:23:58 +08:00
|
|
|
return self.get_template_loaders(self.loaders)
|
|
|
|
|
|
|
|
def get_template_loaders(self, template_loaders):
|
|
|
|
loaders = []
|
|
|
|
for template_loader in template_loaders:
|
|
|
|
loader = self.find_template_loader(template_loader)
|
|
|
|
if loader is not None:
|
|
|
|
loaders.append(loader)
|
|
|
|
return loaders
|
|
|
|
|
|
|
|
def find_template_loader(self, loader):
|
|
|
|
if isinstance(loader, (tuple, list)):
|
|
|
|
args = list(loader[1:])
|
|
|
|
loader = loader[0]
|
|
|
|
else:
|
|
|
|
args = []
|
|
|
|
|
|
|
|
if isinstance(loader, six.string_types):
|
|
|
|
loader_class = import_string(loader)
|
|
|
|
|
|
|
|
if getattr(loader_class, '_accepts_engine_in_init', False):
|
|
|
|
args.insert(0, self)
|
|
|
|
else:
|
|
|
|
warnings.warn(
|
|
|
|
"%s inherits from django.template.loader.BaseLoader "
|
|
|
|
"instead of django.template.loaders.base.Loader. " %
|
2015-06-23 01:54:35 +08:00
|
|
|
loader, RemovedInDjango110Warning, stacklevel=2)
|
2014-11-20 06:23:58 +08:00
|
|
|
|
2015-02-04 05:35:55 +08:00
|
|
|
return loader_class(*args)
|
2014-11-20 06:23:58 +08:00
|
|
|
else:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Invalid value in template loaders configuration: %r" % loader)
|
2014-11-14 22:48:27 +08:00
|
|
|
|
2015-03-04 05:48:26 +08:00
|
|
|
def find_template(self, name, dirs=None, skip=None):
|
|
|
|
tried = []
|
2014-11-14 22:48:27 +08:00
|
|
|
for loader in self.template_loaders:
|
2015-03-04 05:48:26 +08:00
|
|
|
if loader.supports_recursion:
|
|
|
|
try:
|
|
|
|
template = loader.get_template(
|
|
|
|
name, template_dirs=dirs, skip=skip,
|
|
|
|
)
|
|
|
|
return template, template.origin
|
|
|
|
except TemplateDoesNotExist as e:
|
|
|
|
tried.extend(e.tried)
|
|
|
|
else:
|
2015-06-23 01:54:35 +08:00
|
|
|
# RemovedInDjango20Warning: Use old api for non-recursive
|
2015-03-04 05:48:26 +08:00
|
|
|
# loaders.
|
|
|
|
try:
|
|
|
|
return loader(name, dirs)
|
|
|
|
except TemplateDoesNotExist:
|
|
|
|
pass
|
|
|
|
raise TemplateDoesNotExist(name, tried=tried)
|
2014-11-14 22:48:27 +08:00
|
|
|
|
2014-11-29 05:29:45 +08:00
|
|
|
def from_string(self, template_code):
|
2014-11-29 05:33:15 +08:00
|
|
|
"""
|
|
|
|
Returns a compiled Template object for the given template code,
|
|
|
|
handling template inheritance recursively.
|
|
|
|
"""
|
2014-11-29 05:29:45 +08:00
|
|
|
return Template(template_code, engine=self)
|
2014-11-29 05:33:15 +08:00
|
|
|
|
2015-09-04 04:12:22 +08:00
|
|
|
def get_template(self, template_name):
|
2014-11-14 22:48:27 +08:00
|
|
|
"""
|
|
|
|
Returns a compiled Template object for the given template name,
|
|
|
|
handling template inheritance recursively.
|
|
|
|
"""
|
2015-09-04 04:12:22 +08:00
|
|
|
template, origin = self.find_template(template_name)
|
2014-11-14 22:48:27 +08:00
|
|
|
if not hasattr(template, 'render'):
|
|
|
|
# template needs to be compiled
|
2014-11-29 05:29:45 +08:00
|
|
|
template = Template(template, origin, template_name, engine=self)
|
2014-11-14 22:48:27 +08:00
|
|
|
return template
|
|
|
|
|
2014-12-30 06:51:25 +08:00
|
|
|
# This method was originally a function defined in django.template.loader.
|
|
|
|
# It was moved here in Django 1.8 when encapsulating the Django template
|
|
|
|
# engine in this Engine class. It's still called by deprecated code but it
|
2015-06-23 01:54:35 +08:00
|
|
|
# will be removed in Django 1.10. It's superseded by a new render_to_string
|
2014-12-30 06:51:25 +08:00
|
|
|
# function in django.template.loader.
|
|
|
|
|
2014-11-29 06:50:34 +08:00
|
|
|
def render_to_string(self, template_name, context=None,
|
|
|
|
context_instance=_context_instance_undefined,
|
|
|
|
dictionary=_dictionary_undefined):
|
|
|
|
if context_instance is _context_instance_undefined:
|
|
|
|
context_instance = None
|
|
|
|
else:
|
|
|
|
warnings.warn(
|
|
|
|
"The context_instance argument of render_to_string is "
|
2015-06-23 01:54:35 +08:00
|
|
|
"deprecated.", RemovedInDjango110Warning, stacklevel=2)
|
2014-11-29 06:50:34 +08:00
|
|
|
if dictionary is _dictionary_undefined:
|
|
|
|
dictionary = None
|
|
|
|
else:
|
|
|
|
warnings.warn(
|
|
|
|
"The dictionary argument of render_to_string was renamed to "
|
2015-06-23 01:54:35 +08:00
|
|
|
"context.", RemovedInDjango110Warning, stacklevel=2)
|
2014-11-29 06:50:34 +08:00
|
|
|
context = dictionary
|
2014-11-14 22:48:27 +08:00
|
|
|
|
|
|
|
if isinstance(template_name, (list, tuple)):
|
2015-09-04 04:12:22 +08:00
|
|
|
t = self.select_template(template_name)
|
2014-11-14 22:48:27 +08:00
|
|
|
else:
|
2015-09-04 04:12:22 +08:00
|
|
|
t = self.get_template(template_name)
|
2014-11-14 22:48:27 +08:00
|
|
|
if not context_instance:
|
2014-11-29 06:50:34 +08:00
|
|
|
# Django < 1.8 accepted a Context in `context` even though that's
|
|
|
|
# unintended. Preserve this ability but don't rewrap `context`.
|
|
|
|
if isinstance(context, Context):
|
|
|
|
return t.render(context)
|
2014-11-14 22:48:27 +08:00
|
|
|
else:
|
2014-11-29 06:50:34 +08:00
|
|
|
return t.render(Context(context))
|
|
|
|
if not context:
|
2014-11-14 22:48:27 +08:00
|
|
|
return t.render(context_instance)
|
2014-11-29 06:50:34 +08:00
|
|
|
# Add the context to the context stack, ensuring it gets removed again
|
2014-11-14 22:48:27 +08:00
|
|
|
# to keep the context_instance in the same state it started in.
|
2014-11-29 06:50:34 +08:00
|
|
|
with context_instance.push(context):
|
2014-11-14 22:48:27 +08:00
|
|
|
return t.render(context_instance)
|
|
|
|
|
2015-09-04 04:12:22 +08:00
|
|
|
def select_template(self, template_name_list):
|
2014-11-14 22:48:27 +08:00
|
|
|
"""
|
|
|
|
Given a list of template names, returns the first that can be loaded.
|
|
|
|
"""
|
|
|
|
if not template_name_list:
|
|
|
|
raise TemplateDoesNotExist("No template names provided")
|
|
|
|
not_found = []
|
|
|
|
for template_name in template_name_list:
|
|
|
|
try:
|
2015-09-04 04:12:22 +08:00
|
|
|
return self.get_template(template_name)
|
2014-11-14 22:48:27 +08:00
|
|
|
except TemplateDoesNotExist as exc:
|
|
|
|
if exc.args[0] not in not_found:
|
|
|
|
not_found.append(exc.args[0])
|
|
|
|
continue
|
|
|
|
# If we get here, none of the templates could be loaded
|
|
|
|
raise TemplateDoesNotExist(', '.join(not_found))
|