2016-12-31 09:09:26 +08:00
|
|
|
from django.template import Template, TemplateDoesNotExist
|
2014-11-16 01:35:02 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Loader:
|
2014-11-16 01:35:02 +08:00
|
|
|
|
2014-11-20 06:23:58 +08:00
|
|
|
def __init__(self, engine):
|
|
|
|
self.engine = engine
|
2014-11-16 01:35:02 +08:00
|
|
|
|
2016-12-31 09:09:26 +08:00
|
|
|
def get_template(self, template_name, skip=None):
|
2015-03-04 05:48:26 +08:00
|
|
|
"""
|
|
|
|
Calls self.get_template_sources() and returns a Template object for
|
|
|
|
the first template matching template_name. If skip is provided,
|
|
|
|
template origins in skip are ignored. This is used to avoid recursion
|
|
|
|
during template extending.
|
|
|
|
"""
|
|
|
|
tried = []
|
|
|
|
|
2016-12-31 09:09:26 +08:00
|
|
|
for origin in self.get_template_sources(template_name):
|
2015-03-04 05:48:26 +08:00
|
|
|
if skip is not None and origin in skip:
|
|
|
|
tried.append((origin, 'Skipped'))
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
contents = self.get_contents(origin)
|
|
|
|
except TemplateDoesNotExist:
|
|
|
|
tried.append((origin, 'Source does not exist'))
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
return Template(
|
|
|
|
contents, origin, origin.template_name, self.engine,
|
|
|
|
)
|
|
|
|
|
|
|
|
raise TemplateDoesNotExist(template_name, tried=tried)
|
|
|
|
|
|
|
|
def get_template_sources(self, template_name):
|
2014-11-16 01:35:02 +08:00
|
|
|
"""
|
2015-03-04 05:48:26 +08:00
|
|
|
An iterator that yields possible matching template paths for a
|
2014-11-16 01:35:02 +08:00
|
|
|
template name.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError(
|
2015-03-04 05:48:26 +08:00
|
|
|
'subclasses of Loader must provide a get_template_sources() method'
|
|
|
|
)
|
|
|
|
|
2014-11-16 01:35:02 +08:00
|
|
|
def reset(self):
|
|
|
|
"""
|
|
|
|
Resets any state maintained by the loader instance (e.g. cached
|
|
|
|
templates or cached loader modules).
|
|
|
|
"""
|
|
|
|
pass
|