diff options
| author | Georg Bauer <gb@hugo.westfalen.de> | 2005-10-17 11:33:01 +0000 |
|---|---|---|
| committer | Georg Bauer <gb@hugo.westfalen.de> | 2005-10-17 11:33:01 +0000 |
| commit | e8fc1fc97c156035df3e027b1461a1ccad8e96c9 (patch) | |
| tree | 0d708b3450722eba6d19187aefb345e623dacf6e /django/core/template | |
| parent | fcb0aeb5647448902130cb5c3029ac25ee638ee7 (diff) | |
| parent | c686424ed9ba71ddfc6655741c8781b071f08881 (diff) | |
i18n: merged to [897] from trunk
git-svn-id: http://code.djangoproject.com/svn/django/branches/i18n@898 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core/template')
| -rw-r--r-- | django/core/template/loaders/app_directories.py | 28 | ||||
| -rw-r--r-- | django/core/template/loaders/eggs.py | 8 | ||||
| -rw-r--r-- | django/core/template/loaders/filesystem.py | 2 |
3 files changed, 33 insertions, 5 deletions
diff --git a/django/core/template/loaders/app_directories.py b/django/core/template/loaders/app_directories.py new file mode 100644 index 0000000000..5afb18e2f5 --- /dev/null +++ b/django/core/template/loaders/app_directories.py @@ -0,0 +1,28 @@ +# Wrapper for loading templates from "template" directories in installed app packages. + +from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION +from django.core.template import TemplateDoesNotExist +import os + +# At compile time, cache the directories to search. +app_template_dirs = [] +for app in INSTALLED_APPS: + i = app.rfind('.') + m, a = app[:i], app[i+1:] + mod = getattr(__import__(m, '', '', [a]), a) + template_dir = os.path.join(os.path.dirname(mod.__file__), 'templates') + if os.path.isdir(template_dir): + app_template_dirs.append(template_dir) + +# It won't change, so convert it to a tuple to save memory. +app_template_dirs = tuple(app_template_dirs) + +def load_template_source(template_name, template_dirs=None): + for template_dir in app_template_dirs: + filepath = os.path.join(template_dir, template_name) + TEMPLATE_FILE_EXTENSION + try: + return open(filepath).read() + except IOError: + pass + raise TemplateDoesNotExist, template_name +load_template_source.is_usable = True diff --git a/django/core/template/loaders/eggs.py b/django/core/template/loaders/eggs.py index b1e221ee1c..33ba043220 100644 --- a/django/core/template/loaders/eggs.py +++ b/django/core/template/loaders/eggs.py @@ -8,18 +8,18 @@ except ImportError: from django.core.template import TemplateDoesNotExist from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION -def load_template_source(name, dirs=None): +def load_template_source(template_name, template_dirs=None): """ Loads templates from Python eggs via pkg_resource.resource_string. - For every installed app, it tries to get the resource (app, name). + For every installed app, it tries to get the resource (app, template_name). """ if resource_string is not None: - pkg_name = 'templates/' + name + TEMPLATE_FILE_EXTENSION + pkg_name = 'templates/' + template_name + TEMPLATE_FILE_EXTENSION for app in INSTALLED_APPS: try: return resource_string(app, pkg_name) except: pass - raise TemplateDoesNotExist, name + raise TemplateDoesNotExist, template_name load_template_source.is_usable = resource_string is not None diff --git a/django/core/template/loaders/filesystem.py b/django/core/template/loaders/filesystem.py index 1d42c6d841..e5bb1bab1c 100644 --- a/django/core/template/loaders/filesystem.py +++ b/django/core/template/loaders/filesystem.py @@ -17,6 +17,6 @@ def load_template_source(template_name, template_dirs=None): if template_dirs: error_msg = "Tried %s" % tried else: - error_msg = "Your TEMPLATE_DIRS settings is empty. Change it to point to at least one template directory." + error_msg = "Your TEMPLATE_DIRS setting is empty. Change it to point to at least one template directory." raise TemplateDoesNotExist, error_msg load_template_source.is_usable = True |
