Skip to content

Instantly share code, notes, and snippets.

@dchaplinsky
Created October 27, 2010 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dchaplinsky/648887 to your computer and use it in GitHub Desktop.
Save dchaplinsky/648887 to your computer and use it in GitHub Desktop.
Jinja2 loaders for django 1.2
from coffin.template import Template as JTemplate
from django.template.loaders import app_directories, filesystem
from django.template import TemplateDoesNotExist
# integrate jinja instead of django template language to take advantage of
# all django helper function like direct_to_template
def _render_or_die(source, origin):
"""helper function for loaders below. Makes a simple check if
passed template seems to be jinja one and raising exception otherwise"""
if 'jinja' in origin:
# TODO: integration with debug_toolbar
return JTemplate(source)
else:
# This will cause django to try next loader in list
raise TemplateDoesNotExist('Not a jinja template')
class AppDirsLoader(app_directories.Loader):
"""Very simple implementation of the django app_directories loader for jinja"""
is_usable = True
def load_template(self, template_name, template_dirs=None):
source, origin = self.load_template_source(template_name, template_dirs)
return _render_or_die(source, origin), origin
class FileSystemLoader(filesystem.Loader):
"""Very simple implementation of the django filesystem loader for jinja"""
is_usable = True
def load_template(self, template_name, template_dirs=None):
source, origin = self.load_template_source(template_name, template_dirs)
return _render_or_die(source, origin), origin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment