2009-12-14 20:08:23 +08:00
|
|
|
"""
|
|
|
|
Wrapper class that takes a list of template loaders as an argument and attempts
|
|
|
|
to load templates from them in order, caching the result.
|
|
|
|
"""
|
|
|
|
|
2010-05-21 22:25:26 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2010-11-27 13:47:30 +08:00
|
|
|
from django.template.base import TemplateDoesNotExist
|
2009-12-14 20:08:23 +08:00
|
|
|
from django.template.loader import BaseLoader, get_template_from_string, find_template_loader, make_origin
|
2010-05-21 22:25:26 +08:00
|
|
|
from django.utils.hashcompat import sha_constructor
|
2009-12-14 20:08:23 +08:00
|
|
|
from django.utils.importlib import import_module
|
|
|
|
|
|
|
|
class Loader(BaseLoader):
|
|
|
|
is_usable = True
|
|
|
|
|
|
|
|
def __init__(self, loaders):
|
|
|
|
self.template_cache = {}
|
|
|
|
self._loaders = loaders
|
|
|
|
self._cached_loaders = []
|
|
|
|
|
|
|
|
@property
|
|
|
|
def loaders(self):
|
|
|
|
# Resolve loaders on demand to avoid circular imports
|
|
|
|
if not self._cached_loaders:
|
|
|
|
for loader in self._loaders:
|
|
|
|
self._cached_loaders.append(find_template_loader(loader))
|
|
|
|
return self._cached_loaders
|
|
|
|
|
|
|
|
def find_template(self, name, dirs=None):
|
|
|
|
for loader in self.loaders:
|
|
|
|
try:
|
|
|
|
template, display_name = loader(name, dirs)
|
|
|
|
return (template, make_origin(display_name, loader, name, dirs))
|
|
|
|
except TemplateDoesNotExist:
|
|
|
|
pass
|
2010-01-11 02:36:20 +08:00
|
|
|
raise TemplateDoesNotExist(name)
|
2009-12-14 20:08:23 +08:00
|
|
|
|
|
|
|
def load_template(self, template_name, template_dirs=None):
|
2010-05-21 22:25:26 +08:00
|
|
|
key = template_name
|
|
|
|
if template_dirs:
|
|
|
|
# If template directories were specified, use a hash to differentiate
|
|
|
|
key = '-'.join([template_name, sha_constructor('|'.join(template_dirs)).hexdigest()])
|
2010-05-21 16:54:15 +08:00
|
|
|
|
|
|
|
if key not in self.template_cache:
|
2009-12-14 20:08:23 +08:00
|
|
|
template, origin = self.find_template(template_name, template_dirs)
|
|
|
|
if not hasattr(template, 'render'):
|
2010-03-16 22:34:57 +08:00
|
|
|
try:
|
|
|
|
template = get_template_from_string(template, origin, template_name)
|
|
|
|
except TemplateDoesNotExist:
|
2010-05-21 16:54:15 +08:00
|
|
|
# If compiling the template we found raises TemplateDoesNotExist,
|
|
|
|
# back off to returning the source and display name for the template
|
|
|
|
# we were asked to load. This allows for correct identification (later)
|
2010-03-16 22:34:57 +08:00
|
|
|
# of the actual template that does not exist.
|
|
|
|
return template, origin
|
2010-05-21 16:54:15 +08:00
|
|
|
self.template_cache[key] = template
|
|
|
|
return self.template_cache[key], None
|
2009-12-14 20:08:23 +08:00
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
"Empty the template cache."
|
|
|
|
self.template_cache.clear()
|