Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks I mentioned earlier in #flux on IRC #9

Merged
merged 1 commit into from
Jul 22, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions docs/index.rst
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'HELLO WORLD'|truncatewords:"1" }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'GOODBYE CRUEL WORLD'|truncatewords:"1" }}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'hello'.upper() }}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'goodbye'.upper() }}
1 change: 1 addition & 0 deletions jingo/tests/templates/django_app/test_override.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'HELLO WORLD'|truncatewords:"1" }}
1 change: 1 addition & 0 deletions jingo/tests/templates/jinja_app/test_override.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'hello'.upper() }}
20 changes: 20 additions & 0 deletions jingo/tests/test_loader.py
Original file line number Diff line number Diff line change
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')