Fixed #582 -- Added support for loading templates from Python eggs, and a TEMPLATE_LOADERS setting, which defines which loaders to use. Thanks, Sune
git-svn-id: http://code.djangoproject.com/svn/django/trunk@870 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
083b4f9001
commit
7aefff7833
|
@ -61,6 +61,14 @@ TEMPLATE_DIRS = ()
|
|||
# Extension on all templates.
|
||||
TEMPLATE_FILE_EXTENSION = '.html'
|
||||
|
||||
# List of callables that know how to import templates from various sources.
|
||||
# See the comments in django/core/template/loader.py for interface
|
||||
# documentation.
|
||||
TEMPLATE_LOADERS = (
|
||||
'django.core.template.loaders.filesystem.load_template_source',
|
||||
# 'django.core.template.loaders.eggs.load_template_source',
|
||||
)
|
||||
|
||||
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
|
||||
# trailing slash.
|
||||
# Examples: "http://foo.com/media/", "/media/".
|
||||
|
|
|
@ -1,7 +1,50 @@
|
|||
"Wrapper for loading templates from storage of some sort (e.g. files or db)"
|
||||
# Wrapper for loading templates from storage of some sort (e.g. filesystem, database).
|
||||
#
|
||||
# This uses the TEMPLATE_LOADERS setting, which is a list of loaders to use.
|
||||
# Each loader is expected to have this interface:
|
||||
#
|
||||
# callable(name, dirs=[])
|
||||
#
|
||||
# name is the template name.
|
||||
# dirs is an optional list of directories to search instead of TEMPLATE_DIRS.
|
||||
#
|
||||
# Each loader should have an "is_usable" attribute set. This is a boolean that
|
||||
# specifies whether the loader can be used in this Python installation. Each
|
||||
# loader is responsible for setting this when it's initialized.
|
||||
#
|
||||
# For example, the eggs loader (which is capable of loading templates from
|
||||
# Python eggs) sets is_usable to False if the "pkg_resources" module isn't
|
||||
# installed, because pkg_resources is necessary to read eggs.
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.core.template import Template, Context, Node, TemplateDoesNotExist, TemplateSyntaxError, resolve_variable_with_filters, register_tag
|
||||
from django.core.template.loaders.filesystem import load_template_source
|
||||
from django.conf.settings import TEMPLATE_LOADERS
|
||||
|
||||
template_source_loaders = []
|
||||
for path in TEMPLATE_LOADERS:
|
||||
i = path.rfind('.')
|
||||
module, attr = path[:i], path[i+1:]
|
||||
try:
|
||||
mod = __import__(module, globals(), locals(), [attr])
|
||||
except ImportError, e:
|
||||
raise ImproperlyConfigured, 'Error importing template source loader %s: "%s"' % (module, e)
|
||||
try:
|
||||
func = getattr(mod, attr)
|
||||
except AttributeError:
|
||||
raise ImproperlyConfigured, 'Module "%s" does not define a "%s" callable template source loader' % (module, attr)
|
||||
if not func.is_usable:
|
||||
import warnings
|
||||
warnings.warn("Your TEMPLATE_LOADERS setting includes %r, but your Python installation doesn't support that type of template loading. Consider removing that line from TEMPLATE_LOADERS." % path)
|
||||
else:
|
||||
template_source_loaders.append(func)
|
||||
|
||||
def load_template_source(name, dirs=None):
|
||||
for loader in template_source_loaders:
|
||||
try:
|
||||
return loader(name, dirs)
|
||||
except template.TemplateDoesNotExist:
|
||||
pass
|
||||
raise template.TemplateDoesNotExist, name
|
||||
|
||||
class ExtendsError(Exception):
|
||||
pass
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
# Wrapper for loading templates from eggs via pkg_resources.resource_string.
|
||||
|
||||
try:
|
||||
from pkg_resources import resource_string
|
||||
except ImportError:
|
||||
resource_string = None
|
||||
|
||||
from django.core.template import TemplateDoesNotExist
|
||||
from django.conf.settings import INSTALLED_APPS, TEMPLATE_FILE_EXTENSION
|
||||
|
||||
def load_template_source(name, dirs=None):
|
||||
"""
|
||||
Loads templates from Python eggs via pkg_resource.resource_string.
|
||||
|
||||
For every installed app, it tries to get the resource (app, name).
|
||||
"""
|
||||
if resource_string is not None:
|
||||
pkg_name = 'templates/' + name + TEMPLATE_FILE_EXTENSION
|
||||
for app in INSTALLED_APPS:
|
||||
try:
|
||||
return resource_string(app, pkg_name)
|
||||
except:
|
||||
pass
|
||||
raise TemplateDoesNotExist, name
|
||||
load_template_source.is_usable = resource_string is not None
|
|
@ -19,3 +19,4 @@ def load_template_source(template_name, template_dirs=None):
|
|||
else:
|
||||
error_msg = "Your TEMPLATE_DIRS settings is empty. Change it to point to at least one template directory."
|
||||
raise TemplateDoesNotExist, error_msg
|
||||
load_template_source.is_usable = True
|
||||
|
|
Loading…
Reference in New Issue