2014-11-16 01:35:02 +08:00
|
|
|
import warnings
|
|
|
|
|
2015-06-23 01:54:35 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango110Warning
|
2005-10-15 06:22:12 +08:00
|
|
|
|
2014-11-29 05:13:11 +08:00
|
|
|
from . import engines
|
|
|
|
from .backends.django import DjangoTemplates
|
2014-11-29 06:50:34 +08:00
|
|
|
from .engine import (
|
2015-01-28 20:35:27 +08:00
|
|
|
_context_instance_undefined, _dictionary_undefined, _dirs_undefined,
|
|
|
|
)
|
2015-04-25 03:35:30 +08:00
|
|
|
from .exceptions import TemplateDoesNotExist
|
2015-01-28 20:35:27 +08:00
|
|
|
from .loaders import base
|
2014-11-14 03:25:08 +08:00
|
|
|
|
|
|
|
|
2014-11-29 05:13:11 +08:00
|
|
|
def get_template(template_name, dirs=_dirs_undefined, using=None):
|
|
|
|
"""
|
|
|
|
Loads and returns a template for the given name.
|
|
|
|
|
|
|
|
Raises TemplateDoesNotExist if no such template exists.
|
|
|
|
"""
|
2015-04-25 03:33:03 +08:00
|
|
|
chain = []
|
2014-11-29 05:13:11 +08:00
|
|
|
engines = _engine_list(using)
|
|
|
|
for engine in engines:
|
|
|
|
try:
|
|
|
|
# This is required for deprecating the dirs argument. Simply
|
2015-06-23 01:54:35 +08:00
|
|
|
# return engine.get_template(template_name) in Django 1.10.
|
2014-11-29 05:13:11 +08:00
|
|
|
if isinstance(engine, DjangoTemplates):
|
|
|
|
return engine.get_template(template_name, dirs)
|
|
|
|
elif dirs is not _dirs_undefined:
|
|
|
|
warnings.warn(
|
|
|
|
"Skipping template backend %s because its get_template "
|
|
|
|
"method doesn't support the dirs argument." % engine.name,
|
|
|
|
stacklevel=2)
|
|
|
|
else:
|
|
|
|
return engine.get_template(template_name)
|
2015-03-04 05:48:26 +08:00
|
|
|
except TemplateDoesNotExist as e:
|
2015-04-25 03:33:03 +08:00
|
|
|
chain.append(e)
|
2014-11-29 05:13:11 +08:00
|
|
|
|
2015-04-25 03:33:03 +08:00
|
|
|
raise TemplateDoesNotExist(template_name, chain=chain)
|
2014-11-29 05:13:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
def select_template(template_name_list, dirs=_dirs_undefined, using=None):
|
|
|
|
"""
|
|
|
|
Loads and returns a template for one of the given names.
|
|
|
|
|
|
|
|
Tries names in order and returns the first template found.
|
|
|
|
|
|
|
|
Raises TemplateDoesNotExist if no such template exists.
|
|
|
|
"""
|
2015-04-25 03:33:03 +08:00
|
|
|
chain = []
|
2014-11-29 05:13:11 +08:00
|
|
|
engines = _engine_list(using)
|
|
|
|
for template_name in template_name_list:
|
|
|
|
for engine in engines:
|
|
|
|
try:
|
|
|
|
# This is required for deprecating the dirs argument. Simply
|
2015-06-23 01:54:35 +08:00
|
|
|
# use engine.get_template(template_name) in Django 1.10.
|
2014-11-29 05:13:11 +08:00
|
|
|
if isinstance(engine, DjangoTemplates):
|
|
|
|
return engine.get_template(template_name, dirs)
|
|
|
|
elif dirs is not _dirs_undefined:
|
|
|
|
warnings.warn(
|
|
|
|
"Skipping template backend %s because its get_template "
|
|
|
|
"method doesn't support the dirs argument." % engine.name,
|
|
|
|
stacklevel=2)
|
|
|
|
else:
|
|
|
|
return engine.get_template(template_name)
|
2015-03-04 05:48:26 +08:00
|
|
|
except TemplateDoesNotExist as e:
|
2015-04-25 03:33:03 +08:00
|
|
|
chain.append(e)
|
2014-11-29 05:13:11 +08:00
|
|
|
|
|
|
|
if template_name_list:
|
2015-04-25 03:33:03 +08:00
|
|
|
raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
|
2014-11-29 05:13:11 +08:00
|
|
|
else:
|
|
|
|
raise TemplateDoesNotExist("No template names provided")
|
2014-11-14 03:25:08 +08:00
|
|
|
|
|
|
|
|
2014-11-29 06:50:34 +08:00
|
|
|
def render_to_string(template_name, context=None,
|
|
|
|
context_instance=_context_instance_undefined,
|
|
|
|
dirs=_dirs_undefined,
|
|
|
|
dictionary=_dictionary_undefined,
|
2015-01-04 02:06:36 +08:00
|
|
|
request=None, using=None):
|
2014-11-29 06:50:34 +08:00
|
|
|
"""
|
|
|
|
Loads a template and renders it with a context. Returns a string.
|
|
|
|
|
|
|
|
template_name may be a string or a list of strings.
|
|
|
|
"""
|
|
|
|
if (context_instance is _context_instance_undefined
|
|
|
|
and dirs is _dirs_undefined
|
|
|
|
and dictionary is _dictionary_undefined):
|
|
|
|
# No deprecated arguments were passed - use the new code path
|
|
|
|
if isinstance(template_name, (list, tuple)):
|
|
|
|
template = select_template(template_name, using=using)
|
|
|
|
else:
|
|
|
|
template = get_template(template_name, using=using)
|
2015-01-04 02:06:36 +08:00
|
|
|
return template.render(context, request)
|
2014-11-29 06:50:34 +08:00
|
|
|
|
|
|
|
else:
|
2015-04-25 03:33:03 +08:00
|
|
|
chain = []
|
2014-11-29 06:50:34 +08:00
|
|
|
# Some deprecated arguments were passed - use the legacy code path
|
|
|
|
for engine in _engine_list(using):
|
|
|
|
try:
|
2014-12-30 06:51:25 +08:00
|
|
|
# This is required for deprecating properly arguments specific
|
|
|
|
# to Django templates. Remove Engine.render_to_string() at the
|
2015-06-23 01:54:35 +08:00
|
|
|
# same time as this code path in Django 1.10.
|
2014-11-29 06:50:34 +08:00
|
|
|
if isinstance(engine, DjangoTemplates):
|
2015-01-04 02:06:36 +08:00
|
|
|
if request is not None:
|
|
|
|
raise ValueError(
|
|
|
|
"render_to_string doesn't support the request argument "
|
|
|
|
"when some deprecated arguments are passed.")
|
2014-11-29 06:50:34 +08:00
|
|
|
# Hack -- use the internal Engine instance of DjangoTemplates.
|
|
|
|
return engine.engine.render_to_string(
|
|
|
|
template_name, context, context_instance, dirs, dictionary)
|
|
|
|
elif context_instance is not _context_instance_undefined:
|
|
|
|
warnings.warn(
|
|
|
|
"Skipping template backend %s because its render_to_string "
|
|
|
|
"method doesn't support the context_instance argument." %
|
|
|
|
engine.name, stacklevel=2)
|
|
|
|
elif dirs is not _dirs_undefined:
|
|
|
|
warnings.warn(
|
|
|
|
"Skipping template backend %s because its render_to_string "
|
|
|
|
"method doesn't support the dirs argument." % engine.name,
|
|
|
|
stacklevel=2)
|
|
|
|
elif dictionary is not _dictionary_undefined:
|
|
|
|
warnings.warn(
|
|
|
|
"Skipping template backend %s because its render_to_string "
|
|
|
|
"method doesn't support the dictionary argument." %
|
|
|
|
engine.name, stacklevel=2)
|
2015-03-04 05:48:26 +08:00
|
|
|
except TemplateDoesNotExist as e:
|
2015-04-25 03:33:03 +08:00
|
|
|
chain.append(e)
|
2014-11-29 06:50:34 +08:00
|
|
|
continue
|
|
|
|
|
|
|
|
if template_name:
|
|
|
|
if isinstance(template_name, (list, tuple)):
|
|
|
|
template_name = ', '.join(template_name)
|
2015-04-25 03:33:03 +08:00
|
|
|
raise TemplateDoesNotExist(template_name, chain=chain)
|
2014-11-29 06:50:34 +08:00
|
|
|
else:
|
|
|
|
raise TemplateDoesNotExist("No template names provided")
|
2014-11-14 22:48:27 +08:00
|
|
|
|
2014-11-14 03:25:08 +08:00
|
|
|
|
2014-11-29 05:13:11 +08:00
|
|
|
def _engine_list(using=None):
|
|
|
|
return engines.all() if using is None else [engines[using]]
|
2014-11-16 01:35:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
class BaseLoader(base.Loader):
|
2014-11-20 06:23:58 +08:00
|
|
|
_accepts_engine_in_init = False
|
2014-11-16 01:35:02 +08:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
warnings.warn(
|
2014-11-20 06:23:58 +08:00
|
|
|
"django.template.loader.BaseLoader was superseded by "
|
2014-11-16 01:35:02 +08:00
|
|
|
"django.template.loaders.base.Loader.",
|
2015-06-23 01:54:35 +08:00
|
|
|
RemovedInDjango110Warning, stacklevel=2)
|
2014-11-16 01:35:02 +08:00
|
|
|
super(BaseLoader, self).__init__(*args, **kwargs)
|