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