2015-09-27 01:38:04 +08:00
|
|
|
from django.utils import six
|
|
|
|
|
2014-11-29 05:13:11 +08:00
|
|
|
from . import engines
|
2015-04-25 03:35:30 +08:00
|
|
|
from .exceptions import TemplateDoesNotExist
|
2014-11-14 03:25:08 +08:00
|
|
|
|
|
|
|
|
2015-09-04 04:12:22 +08:00
|
|
|
def get_template(template_name, using=None):
|
2014-11-29 05:13:11 +08:00
|
|
|
"""
|
|
|
|
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:
|
2015-09-04 04:12:22 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
2015-09-04 04:12:22 +08:00
|
|
|
def select_template(template_name_list, using=None):
|
2014-11-29 05:13:11 +08:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2016-01-21 16:40:33 +08:00
|
|
|
if isinstance(template_name_list, six.string_types):
|
|
|
|
raise TypeError(
|
|
|
|
'select_template() takes an iterable of template names but got a '
|
|
|
|
'string: %r. Use get_template() if you want to load a single '
|
|
|
|
'template by name.' % template_name_list
|
|
|
|
)
|
|
|
|
|
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:
|
2015-09-04 04:12:22 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
2015-09-04 10:01:30 +08:00
|
|
|
def render_to_string(template_name, context=None, 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.
|
|
|
|
"""
|
2015-09-04 10:01:30 +08:00
|
|
|
if isinstance(template_name, (list, tuple)):
|
|
|
|
template = select_template(template_name, using=using)
|
2014-11-29 06:50:34 +08:00
|
|
|
else:
|
2015-09-04 10:01:30 +08:00
|
|
|
template = get_template(template_name, using=using)
|
|
|
|
return template.render(context, request)
|
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]]
|