2014-11-16 01:35:02 +08:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2005-10-15 06:22:12 +08:00
|
|
|
|
2014-11-29 05:13:11 +08:00
|
|
|
from . import engines
|
|
|
|
from .backends.django import DjangoTemplates
|
|
|
|
from .base import Origin, TemplateDoesNotExist
|
|
|
|
from .engine import _dirs_undefined, Engine
|
2014-11-14 03:25:08 +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
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
engines = _engine_list(using)
|
|
|
|
for engine in engines:
|
|
|
|
try:
|
|
|
|
# This is required for deprecating the dirs argument. Simply
|
|
|
|
# return engine.get_template(template_name) in Django 2.0.
|
|
|
|
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)
|
|
|
|
except TemplateDoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
raise TemplateDoesNotExist(template_name)
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
# use engine.get_template(template_name) in Django 2.0.
|
|
|
|
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)
|
|
|
|
except TemplateDoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if template_name_list:
|
|
|
|
raise TemplateDoesNotExist(', '.join(template_name_list))
|
|
|
|
else:
|
|
|
|
raise TemplateDoesNotExist("No template names provided")
|
2014-11-14 03:25:08 +08:00
|
|
|
|
|
|
|
|
2014-11-14 22:48:27 +08:00
|
|
|
def render_to_string(*args, **kwargs):
|
|
|
|
return Engine.get_default().render_to_string(*args, **kwargs)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
# This line must remain at the bottom to avoid import loops.
|
|
|
|
from .loaders import base
|
|
|
|
|
|
|
|
|
|
|
|
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.",
|
|
|
|
RemovedInDjango20Warning, stacklevel=2)
|
|
|
|
super(BaseLoader, self).__init__(*args, **kwargs)
|