equal
deleted
inserted
replaced
|
1 """ |
|
2 Wrapper for loading templates from the filesystem. |
|
3 """ |
|
4 |
|
5 from django.conf import settings |
|
6 from django.template import TemplateDoesNotExist |
|
7 from django.utils._os import safe_join |
|
8 |
|
9 def get_template_sources(template_name, template_dirs=None): |
|
10 if not template_dirs: |
|
11 template_dirs = settings.TEMPLATE_DIRS |
|
12 for template_dir in template_dirs: |
|
13 try: |
|
14 yield safe_join(template_dir, template_name) |
|
15 except ValueError: |
|
16 # The joined path was located outside of template_dir. |
|
17 pass |
|
18 |
|
19 def load_template_source(template_name, template_dirs=None): |
|
20 tried = [] |
|
21 for filepath in get_template_sources(template_name, template_dirs): |
|
22 try: |
|
23 return (open(filepath).read().decode(settings.FILE_CHARSET), filepath) |
|
24 except IOError: |
|
25 tried.append(filepath) |
|
26 if tried: |
|
27 error_msg = "Tried %s" % tried |
|
28 else: |
|
29 error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory." |
|
30 raise TemplateDoesNotExist, error_msg |
|
31 load_template_source.is_usable = True |