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
|
|
|
"""
|
2017-01-25 04:36:36 +08:00
|
|
|
Load and return a template for the given name.
|
2014-11-29 05:13:11 +08:00
|
|
|
|
2017-01-25 04:36:36 +08:00
|
|
|
Raise TemplateDoesNotExist if no such template exists.
|
2014-11-29 05:13:11 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
2017-01-25 04:36:36 +08:00
|
|
|
Load and return a template for one of the given names.
|
2014-11-29 05:13:11 +08:00
|
|
|
|
2017-01-25 04:36:36 +08:00
|
|
|
Try names in order and return the first template found.
|
2014-11-29 05:13:11 +08:00
|
|
|
|
2017-01-25 04:36:36 +08:00
|
|
|
Raise TemplateDoesNotExist if no such template exists.
|
2014-11-29 05:13:11 +08:00
|
|
|
"""
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(template_name_list, str):
|
2016-01-21 16:40:33 +08:00
|
|
|
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
|
|
|
"""
|
2017-01-25 04:36:36 +08:00
|
|
|
Load a template and render it with a context. Return a string.
|
2014-11-29 06:50:34 +08:00
|
|
|
|
|
|
|
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]]
|