Refactor extension loading slightly.
This commit is contained in:
parent
df814b1b93
commit
d75943cb42
@ -73,7 +73,6 @@ class CoffinEnvironment(Environment):
|
||||
warnings.warn('Cannot translate loader: %s' % loader)
|
||||
return loaders
|
||||
|
||||
|
||||
def _get_templatelibs(self):
|
||||
"""Return an iterable of template ``Library`` instances.
|
||||
|
||||
@ -81,30 +80,35 @@ class CoffinEnvironment(Environment):
|
||||
register all libraries globally.
|
||||
"""
|
||||
from django.conf import settings
|
||||
from django.template import get_library, InvalidTemplateLibrary
|
||||
from django.template import (
|
||||
get_library, import_library, InvalidTemplateLibrary)
|
||||
|
||||
libs = []
|
||||
for a in settings.INSTALLED_APPS:
|
||||
for app in settings.INSTALLED_APPS:
|
||||
ns = app + '.templatetags'
|
||||
try:
|
||||
path = __import__(a + '.templatetags', {}, {}, ['__file__']).__file__
|
||||
path = __import__(ns, {}, {}, ['__file__']).__file__
|
||||
path = os.path.dirname(path) # we now have the templatetags/ directory
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
for f in os.listdir(path):
|
||||
if f == '__init__.py' or f.startswith('.'):
|
||||
for filename in os.listdir(path):
|
||||
if filename == '__init__.py' or filename.startswith('.'):
|
||||
continue
|
||||
|
||||
if f.endswith('.py'):
|
||||
if filename.endswith('.py'):
|
||||
try:
|
||||
# TODO: will need updating when #6587 lands
|
||||
# libs.append(get_library(
|
||||
# "django.templatetags.%s" % os.path.splitext(f)[0]))
|
||||
l = get_library(os.path.splitext(f)[0])
|
||||
module = "%s.%s" % (ns, os.path.splitext(filename)[0])
|
||||
l = import_library(module)
|
||||
libs.append(l)
|
||||
|
||||
except InvalidTemplateLibrary:
|
||||
pass
|
||||
|
||||
# In addition to loading application libraries, support a custom list
|
||||
for libname in getattr(settings, 'JINJA2_DJANGO_TEMPLATETAG_LIBRARIES', ()):
|
||||
libs.append(get_library(libname))
|
||||
|
||||
return libs
|
||||
|
||||
def _get_all_extensions(self):
|
||||
@ -168,19 +172,10 @@ class CoffinEnvironment(Environment):
|
||||
tests.update(from_setting('JINJA2_TESTS', True))
|
||||
filters.update(from_setting('JINJA2_FILTERS', True))
|
||||
globals.update(from_setting('JINJA2_GLOBALS'))
|
||||
|
||||
|
||||
# Finally, add extensions defined in application's templatetag libraries
|
||||
libraries = self._get_templatelibs()
|
||||
|
||||
# Load custom libraries.
|
||||
from django.template import get_library
|
||||
for libname in getattr(settings, 'JINJA2_DJANGO_TEMPLATETAG_LIBRARIES', ()):
|
||||
libraries.append(get_library(libname))
|
||||
|
||||
for lib in libraries:
|
||||
for lib in self._get_templatelibs():
|
||||
_load_lib(lib)
|
||||
attrs.update(getattr(lib, 'jinja2_environment_attrs', {}))
|
||||
|
||||
return dict(
|
||||
extensions=extensions,
|
||||
|
@ -88,6 +88,10 @@ def add_to_builtins(module_name):
|
||||
You can still use Django's own ``add_to_builtins`` to register
|
||||
directly with Django and bypass Coffin.
|
||||
|
||||
Once thing that is special about Coffin is that because {% load %}
|
||||
is not supported in Coffin, *everything* it provides must be
|
||||
registered through the builtins.
|
||||
|
||||
TODO: Allow passing path to (or reference of) extensions and
|
||||
filters directly. This would make it easier to use this function
|
||||
with 3rd party Jinja extensions that do not know about Coffin and
|
||||
|
Loading…
Reference in New Issue
Block a user