Skip to content

Commit

Permalink
Merge pull request #31 from akx/master
Browse files Browse the repository at this point in the history
Non-callable globals, custom Django templatetag libraries, *.jinja loader
  • Loading branch information
miracle2k committed May 30, 2012
2 parents bf1fc6a + 7ec1c21 commit 2184c2a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
24 changes: 18 additions & 6 deletions coffin/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,23 +150,35 @@ def _load_lib(lib):

# Next, add the globally defined extensions
extensions.extend(list(getattr(settings, 'JINJA2_EXTENSIONS', [])))
def from_setting(setting):
def from_setting(setting, values_must_be_callable = False):
retval = {}
setting = getattr(settings, setting, {})
if isinstance(setting, dict):
for key, value in setting.iteritems():
retval[key] = callable(value) and value or get_callable(value)
if values_must_be_callable and not callable(value):
value = get_callable(value)
retval[key] = value
else:
for value in setting:
value = callable(value) and value or get_callable(value)
if values_must_be_callable and not callable(value):
value = get_callable(value)
retval[value.__name__] = value
return retval
filters.update(from_setting('JINJA2_FILTERS'))

tests.update(from_setting('JINJA2_TESTS', True))
filters.update(from_setting('JINJA2_FILTERS', True))
globals.update(from_setting('JINJA2_GLOBALS'))
tests.update(from_setting('JINJA2_TESTS'))


# Finally, add extensions defined in application's templatetag libraries
for lib in self._get_templatelibs():
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:
_load_lib(lib)
attrs.update(getattr(lib, 'jinja2_environment_attrs', {}))

Expand Down
41 changes: 41 additions & 0 deletions coffin/contrib/loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""
A Django template loader wrapper for Coffin that intercepts
requests for "*.jinja" templates, rendering them with Coffin
instead of Django templates.
Usage:
TEMPLATE_LOADERS = (
'coffin.contrib.loader.AppLoader',
'coffin.contrib.loader.FileSystemLoader',
)
"""

from coffin.common import env
from django.conf import settings
from django.template.loaders import app_directories, filesystem


JINJA2_DEFAULT_TEMPLATE_EXTENSION = getattr(settings,
'JINJA2_DEFAULT_TEMPLATE_EXTENSION', '.jinja')


class LoaderMixin(object):
is_usable = True

def load_template(self, template_name, template_dirs=None):
if not template_name.endswith(JINJA2_DEFAULT_TEMPLATE_EXTENSION):
return super(LoaderMixin, self).load_template(template_name,
template_dirs)
template = env.get_template(template_name)
return template, template.filename


class FileSystemLoader(LoaderMixin, filesystem.Loader):
pass


class AppLoader(LoaderMixin, app_directories.Loader):
pass

0 comments on commit 2184c2a

Please sign in to comment.