Jinja2 Django integration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from django.http import HttpResponse
from django.conf import settings
from jinja2 import PackageLoader, Environment, ChoiceLoader, FileSystemLoader
import os
from django.core.urlresolvers import get_callable
from djangotags import *
from django.utils import translation
from django.utils.thread_support import currentThread

global env

# Setup template loaders

loader_array = []
for pth in getattr(settings, 'TEMPLATE_DIRS', ()):
    loader_array.append(FileSystemLoader(pth))

for app in settings.INSTALLED_APPS:
    loader_array.append(PackageLoader(app))

# Setup environment

default_mimetype = getattr(settings, 'DEFAULT_CONTENT_TYPE')

global_exts = getattr(settings, 'JINJA_EXTS', ())

env = Environment(extensions=global_exts, loader=ChoiceLoader(loader_array))

if 'jinja2.ext.i18n' in global_exts:
    env.install_gettext_translations(translation)

# Add user Globals, Filters, Tests
global_imports = getattr(settings, 'JINJA_GLOBALS', ())
for imp in global_imports:
    method = get_callable(imp)
    method_name = getattr(method,'jinja_name',None)
    if not method_name == None:
        env.globals[method_name] = method
    else:
        env.globals[method.__name__] = method

global_filters = getattr(settings, 'JINJA_FILTERS', ())
for imp in global_filters:
    method = get_callable(imp)
    method_name = getattr(method,'jinja_name',None)
    if not method_name == None:
        env.filters[method_name] = method
    else:
        env.filters[method.__name__] = method

global_tests = getattr(settings, 'JINJA_TESTS', ())
for imp in global_tests:
    method = get_callable(imp)
    method_name = getattr(method,'jinja_name',None)
    if not method_name == None:
        env.tests[method_name] = method
    else:
        env.tests[method.__name__] = method


def render_to_string(filename, context={}):
    template = env.get_template(filename)
    rendered = template.render(**context)
    return rendered

def render_to_response(filename, context={},mimetype=default_mimetype, request = None):
    if request:
        context['request'] = request
    rendered = render_to_string(filename, context)
    return HttpResponse(rendered,mimetype=mimetype)

More like this

  1. Django 1.2+ template loader for Jinja2 by SimonSapin 3 years, 10 months ago
  2. Jinja2 integration + application specific functions/filters/tests by hasenj 5 years, 6 months ago
  3. integrated jinja2 which could use generic view ,my djangojinja2.py by jasongreen 4 years, 3 months ago
  4. monkey-patch django to use jinja2 templates for 404/500 pages and 3rd-party apps by brondsem 5 years, 1 month ago
  5. Using Django Generics with Jinja2 by rmt 5 years, 4 months ago

Comments

mathwizard (on September 18, 2008):

Hello, thanks, I haven't been aware of that script, but I personally like mine better, as it automatically includes each applications template dir, and lets you specify custom filters, tests, etc in your project settings.

#

(Forgotten your password?)