Permalink
Browse files

Merge pull request #9 from lmorchard/lmo-loader-chain

Tweaks I mentioned earlier in #flux on IRC
  • Loading branch information...
2 parents d3b4e9f + 0bb713a commit b4ecca1066bcd1eac87358b4c82902442d4eedc4 @davedash davedash committed Jul 22, 2011
View
@@ -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
View
@@ -3,9 +3,14 @@
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',)
View
@@ -5,8 +5,9 @@
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 @@ def render(self, context):
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
No changes.
@@ -0,0 +1 @@
+{{ 'HELLO WORLD'|truncatewords:"1" }}
@@ -0,0 +1 @@
+{{ 'GOODBYE CRUEL WORLD'|truncatewords:"1" }}
No changes.
@@ -0,0 +1 @@
+{{ 'hello'.upper() }}
@@ -0,0 +1 @@
+{{ 'goodbye'.upper() }}
@@ -0,0 +1 @@
+{{ 'HELLO WORLD'|truncatewords:"1" }}
@@ -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')
+

0 comments on commit b4ecca1

Please sign in to comment.