Skip to content

Commit

Permalink
Merge pull request #9 from lmorchard/lmo-loader-chain
Browse files Browse the repository at this point in the history
Tweaks I mentioned earlier in #flux on IRC
  • Loading branch information
davedash committed Jul 22, 2011
2 parents d3b4e9f + 0bb713a commit b4ecca1
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 12 deletions.
12 changes: 7 additions & 5 deletions docs/index.rst
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions fake_settings.py
Expand Up @@ -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',)
10 changes: 5 additions & 5 deletions jingo/__init__.py
Expand Up @@ -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

Expand Down Expand Up @@ -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
Empty file.
@@ -0,0 +1 @@
{{ 'HELLO WORLD'|truncatewords:"1" }}
@@ -0,0 +1 @@
{{ 'GOODBYE CRUEL WORLD'|truncatewords:"1" }}
Empty file.
@@ -0,0 +1 @@
{{ 'hello'.upper() }}
@@ -0,0 +1 @@
{{ 'goodbye'.upper() }}
1 change: 1 addition & 0 deletions jingo/tests/templates/django_app/test_override.html
@@ -0,0 +1 @@
{{ 'HELLO WORLD'|truncatewords:"1" }}
1 change: 1 addition & 0 deletions jingo/tests/templates/jinja_app/test_override.html
@@ -0,0 +1 @@
{{ 'hello'.upper() }}
20 changes: 20 additions & 0 deletions jingo/tests/test_loader.py
Expand Up @@ -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.