From 57b4f231fdc24dcb41a503c3314442ff5250a830 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Mon, 17 Oct 2005 02:37:50 +0000 Subject: [PATCH] Fixed #583 -- Added app_directories template loader, which searches for templates in 'templates' directory in each INSTALLED_APPS package. It's turned off by default. git-svn-id: http://code.djangoproject.com/svn/django/trunk@892 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/conf/global_settings.py | 1 + django/conf/project_template/settings/main.py | 7 +++++ .../core/template/loaders/app_directories.py | 28 +++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 django/core/template/loaders/app_directories.py diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index 41fe147374..f0f66540db 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -65,6 +65,7 @@ TEMPLATE_FILE_EXTENSION = '.html' # See the comments in django/core/template/loader.py for interface # documentation. TEMPLATE_LOADERS = ( +# 'django.core.template.loaders.app_directories.load_template_source', 'django.core.template.loaders.filesystem.load_template_source', # 'django.core.template.loaders.eggs.load_template_source', ) diff --git a/django/conf/project_template/settings/main.py b/django/conf/project_template/settings/main.py index cbb32b7920..1bde7df10a 100644 --- a/django/conf/project_template/settings/main.py +++ b/django/conf/project_template/settings/main.py @@ -30,6 +30,13 @@ MEDIA_URL = '' # Make this unique, and don't share it with anybody. SECRET_KEY = '' +# List of callables that know how to import templates from various sources. +TEMPLATE_LOADERS = ( +# 'django.core.template.loaders.app_directories.load_template_source', + 'django.core.template.loaders.filesystem.load_template_source', +# 'django.core.template.loaders.eggs.load_template_source', +) + MIDDLEWARE_CLASSES = ( "django.middleware.common.CommonMiddleware", "django.middleware.doc.XViewMiddleware", 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