Skip to content
Myr

added djangojinja2 extension

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent ba80826 · Browse files · 1 file+85 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+85 added−0 removed

  • other:+85 added−0 removed

1 file+85 added−0 removed

added:ext/djangojinja2.py+85 added
1# -*- coding: utf-8 -*-2"""3 djangojinja24 ~~~~~~~~~~~~56 Adds support for Jinja2 to Django.78 Configuration variables:910 ======================= =============================================11 Key Description12 ======================= =============================================13 `JINJA2_TEMPLATE_DIRS` List of template folders14 `JINJA2_EXTENSIONS` List of Jinja2 extensions to use15 `JINJA2_CACHE_SIZE` The size of the Jinja2 template cache.16 ======================= =============================================1718 :copyright: Copyright 2008 by Armin Ronacher.19 :license: BSD.20"""21from itertools import chain22from django.conf import settings23from django.core.exceptions import ImproperlyConfigured24from django.template.context import get_standard_processors25from django.template import TemplateDoesNotExist26from jinja2 import Environment, FileSystemLoader, TemplateNotFound27from jinja2.defaults import DEFAULT_NAMESPACE282930# the environment is unconfigured until the first template is loaded.31_jinja_env = None323334def get_env():35 """Get the Jinja2 env and initialize it if necessary."""36 global _jinja_env37 if _jinja_env is None:38 _jinja_env = create_env()39 return _jinja_env404142def create_env():43 """Create a new Jinja2 environment."""44 searchpath = list(settings.JINJA2_TEMPLATE_DIRS)45 return Environment(loader=FileSystemLoader(searchpath),46 auto_reload=not settings.TEMPLATE_DEBUG,47 cache_size=getattr(settings, 'JINJA2_CACHE_SIZE', 50),48 extensions=getattr(settings, 'JINJA2_EXTENSIONS', ()))495051def get_template(template_name, globals=None):52 """Load a template."""53 try:54 return get_env().get_template(template_name, globals=globals)55 except TemplateNotFound, e:56 raise TemplateDoesNotExist(str(e))575859def select_template(templates, globals=None):60 """Try to load one of the given templates."""61 env = get_env()62 for template in templates:63 try:64 return env.get_template(template, globals=globals)65 except TemplateNotFound:66 continue67 raise TemplateDoesNotExist(', '.join(templates))686970def render_to_string(template_name, context=None, request=None,71 processors=None):72 """Render a template into a string."""73 context = dict(context or {})74 if request is not None:75 context['request'] = request76 for processor in chain(get_standard_processors(), processors or ()):77 context.update(processor(request))78 return get_template(template_name).render(context)798081def render_to_response(template_name, context=None, request=None,82 processors=None, mimetype=None):83 """Render a template into a response object."""84 return HttpResponse(render_to_string(template_name, context, request,85 processors), mimetype=None)