You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base.py 1.5KB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from django.template import Template, TemplateDoesNotExist
  2. class Loader:
  3. def __init__(self, engine):
  4. self.engine = engine
  5. def get_template(self, template_name, skip=None):
  6. """
  7. Call self.get_template_sources() and return a Template object for
  8. the first template matching template_name. If skip is provided, ignore
  9. template origins in skip. This is used to avoid recursion during
  10. template extending.
  11. """
  12. tried = []
  13. for origin in self.get_template_sources(template_name):
  14. if skip is not None and origin in skip:
  15. tried.append((origin, 'Skipped'))
  16. continue
  17. try:
  18. contents = self.get_contents(origin)
  19. except TemplateDoesNotExist:
  20. tried.append((origin, 'Source does not exist'))
  21. continue
  22. else:
  23. return Template(
  24. contents, origin, origin.template_name, self.engine,
  25. )
  26. raise TemplateDoesNotExist(template_name, tried=tried)
  27. def get_template_sources(self, template_name):
  28. """
  29. An iterator that yields possible matching template paths for a
  30. template name.
  31. """
  32. raise NotImplementedError(
  33. 'subclasses of Loader must provide a get_template_sources() method'
  34. )
  35. def reset(self):
  36. """
  37. Reset any state maintained by the loader instance (e.g. cached
  38. templates or cached loader modules).
  39. """
  40. pass