Make jingo.Loader properly fall back to other TEMPLATE_LOADERS
- jingo.Loader raises TemplateDoesNotExist when excluded app encountered, which causes fallback to the next loader listed in settings.TEMPLATE_LOADERS - renamed setting DJANGO_TEMPLATE_APPS -> JINGO_EXCLUDE_APPS - tests updated to ensure top-level override of app-level templates works for both Jinja and Django loaders
This commit is contained in:
@@ -31,7 +31,8 @@ You'll want to use jingo's template loader::
|
||||
|
||||
TEMPLATE_LOADERS = (
|
||||
'jingo.Loader',
|
||||
'django.template.loaders.filesystem.Loader'
|
||||
'django.template.loaders.filesystem.Loader',
|
||||
'django.template.loaders.app_directories.Loader',
|
||||
)
|
||||
|
||||
This will let you use ``django.shortcuts.render`` or
|
||||
@@ -40,11 +41,12 @@ This will let you use ``django.shortcuts.render`` or
|
||||
And finally you may have apps that do not use Jinja2, these must be excluded
|
||||
from the loader::
|
||||
|
||||
DJANGO_TEMPLATE_APPS = ('debug_toolbar',)
|
||||
JINGO_EXCLUDE_APPS = ('debug_toolbar',)
|
||||
|
||||
If a template is in the *app folder*, `debug_toolbar`, Django will handle the
|
||||
templating, not Jinja. If this fails, Django will then move on to the next
|
||||
loader.
|
||||
If a template is in the *app folder*, `debug_toolbar`, the Jinja loader will
|
||||
raise a TemplateDoesNotExist exception. This causes Django to move onto the
|
||||
next loader in TEMPLATE_LOADERS to find a template - in this case,
|
||||
``django.template.loaders.filesystem.Loader``
|
||||
|
||||
|
||||
Template Helpers
|
||||
|
||||
@@ -3,9 +3,14 @@ import os
|
||||
path = lambda *a: os.path.join(ROOT, *a)
|
||||
|
||||
ROOT = os.path.dirname(os.path.abspath(__file__))
|
||||
INSTALLED_APPS = (
|
||||
'jingo.tests.jinja_app',
|
||||
'jingo.tests.django_app'
|
||||
)
|
||||
TEMPLATE_LOADERS = (
|
||||
'jingo.Loader',
|
||||
'django.template.loaders.filesystem.Loader'
|
||||
'django.template.loaders.filesystem.Loader',
|
||||
'django.template.loaders.app_directories.Loader',
|
||||
)
|
||||
TEMPLATE_DIRS = (path('jingo/tests/templates'),)
|
||||
DJANGO_TEMPLATE_APPS = ('django_app',)
|
||||
JINGO_EXCLUDE_APPS = ('django_app',)
|
||||
|
||||
@@ -5,8 +5,9 @@ import logging
|
||||
|
||||
from django import http
|
||||
from django.conf import settings
|
||||
from django.template.base import TemplateDoesNotExist
|
||||
from django.template.context import get_standard_processors
|
||||
from django.template.loaders.app_directories import Loader as AppLoader
|
||||
from django.template.loader import BaseLoader
|
||||
from django.utils.importlib import import_module
|
||||
from django.utils.translation import trans_real
|
||||
|
||||
@@ -165,15 +166,14 @@ class Template(object):
|
||||
return self.template.render(context_dict)
|
||||
|
||||
|
||||
class Loader(AppLoader):
|
||||
class Loader(BaseLoader):
|
||||
is_usable = True
|
||||
|
||||
def load_template(self, template_name, template_dirs=None):
|
||||
if hasattr(template_name, 'rsplit'):
|
||||
app = template_name.rsplit('/')[0]
|
||||
if app in getattr(settings, 'DJANGO_TEMPLATE_APPS', []):
|
||||
return super(Loader, self).load_template(
|
||||
template_name, template_dirs)
|
||||
if app in getattr(settings, 'JINGO_EXCLUDE_APPS', []):
|
||||
raise TemplateDoesNotExist(template_name)
|
||||
|
||||
template = env.get_template(template_name)
|
||||
return Template(template), template.filename
|
||||
|
||||
0
jingo/tests/django_app/__init__.py
Normal file
0
jingo/tests/django_app/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
{{ 'HELLO WORLD'|truncatewords:"1" }}
|
||||
@@ -0,0 +1 @@
|
||||
{{ 'GOODBYE CRUEL WORLD'|truncatewords:"1" }}
|
||||
0
jingo/tests/jinja_app/__init__.py
Normal file
0
jingo/tests/jinja_app/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
{{ 'hello'.upper() }}
|
||||
@@ -0,0 +1 @@
|
||||
{{ 'goodbye'.upper() }}
|
||||
1
jingo/tests/templates/django_app/test_override.html
Normal file
1
jingo/tests/templates/django_app/test_override.html
Normal file
@@ -0,0 +1 @@
|
||||
{{ 'HELLO WORLD'|truncatewords:"1" }}
|
||||
1
jingo/tests/templates/jinja_app/test_override.html
Normal file
1
jingo/tests/templates/jinja_app/test_override.html
Normal file
@@ -0,0 +1 @@
|
||||
{{ 'hello'.upper() }}
|
||||
@@ -11,7 +11,27 @@ def test_render():
|
||||
eq_(r.content, 'HELLO')
|
||||
|
||||
|
||||
def test_render_no_toplevel_override():
|
||||
r = render(Mock(), 'jinja_app/test_nonoverride.html', {})
|
||||
eq_(r.content, 'HELLO')
|
||||
|
||||
|
||||
def test_render_toplevel_override():
|
||||
r = render(Mock(), 'jinja_app/test_override.html', {})
|
||||
eq_(r.content, 'HELLO')
|
||||
|
||||
|
||||
def test_render_django():
|
||||
r = render(Mock(), 'django_app/test.html', {})
|
||||
eq_(r.content, 'HELLO ...\n')
|
||||
|
||||
|
||||
def test_render_django_no_toplevel_override():
|
||||
r = render(Mock(), 'django_app/test_nonoverride.html', {})
|
||||
eq_(r.content, 'HELLO ...\n')
|
||||
|
||||
|
||||
def test_render_django_toplevel_override():
|
||||
r = render(Mock(), 'django_app/test_override.html', {})
|
||||
eq_(r.content, 'HELLO ...\n')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user