2007-07-23 12:45:01 +08:00
|
|
|
"""
|
|
|
|
Wrapper for loading templates from the filesystem.
|
|
|
|
"""
|
2005-09-29 23:06:53 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.template import TemplateDoesNotExist
|
2007-07-23 12:45:01 +08:00
|
|
|
from django.utils._os import safe_join
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-25 05:14:42 +08:00
|
|
|
def get_template_sources(template_name, template_dirs=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
if not template_dirs:
|
2006-05-02 09:31:56 +08:00
|
|
|
template_dirs = settings.TEMPLATE_DIRS
|
2005-07-13 09:25:57 +08:00
|
|
|
for template_dir in template_dirs:
|
2007-07-23 12:45:01 +08:00
|
|
|
try:
|
|
|
|
yield safe_join(template_dir, template_name)
|
|
|
|
except ValueError:
|
|
|
|
# The joined path was located outside of template_dir.
|
|
|
|
pass
|
2005-11-25 05:14:42 +08:00
|
|
|
|
|
|
|
def load_template_source(template_name, template_dirs=None):
|
|
|
|
tried = []
|
|
|
|
for filepath in get_template_sources(template_name, template_dirs):
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return (open(filepath).read().decode(settings.FILE_CHARSET), filepath)
|
2005-07-13 09:25:57 +08:00
|
|
|
except IOError:
|
|
|
|
tried.append(filepath)
|
2006-08-13 09:49:11 +08:00
|
|
|
if tried:
|
2005-07-19 12:40:57 +08:00
|
|
|
error_msg = "Tried %s" % tried
|
|
|
|
else:
|
2005-10-17 10:18:28 +08:00
|
|
|
error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory."
|
2005-07-19 12:40:57 +08:00
|
|
|
raise TemplateDoesNotExist, error_msg
|
2005-10-15 06:22:12 +08:00
|
|
|
load_template_source.is_usable = True
|