autoescape no longer is a plain boolean value but can also be a function
[jinja2.git] / ext / djangojinja2.py
1 # -*- coding: utf-8 -*-
2 """
3     djangojinja2
4     ~~~~~~~~~~~~
5
6     Adds support for Jinja2 to Django.
7
8     Configuration variables:
9
10     ======================= =============================================
11     Key                     Description
12     ======================= =============================================
13     `JINJA2_TEMPLATE_DIRS`  List of template folders
14     `JINJA2_EXTENSIONS`     List of Jinja2 extensions to use
15     `JINJA2_CACHE_SIZE`     The size of the Jinja2 template cache.
16     ======================= =============================================
17
18     :copyright: (c) 2009 by the Jinja Team.
19     :license: BSD.
20 """
21 from itertools import chain
22 from django.conf import settings
23 from django.http import HttpResponse
24 from django.core.exceptions import ImproperlyConfigured
25 from django.template.context import get_standard_processors
26 from django.template import TemplateDoesNotExist
27 from jinja2 import Environment, FileSystemLoader, TemplateNotFound
28 from jinja2.defaults import DEFAULT_NAMESPACE
29
30
31 # the environment is unconfigured until the first template is loaded.
32 _jinja_env = None
33
34
35 def get_env():
36     """Get the Jinja2 env and initialize it if necessary."""
37     global _jinja_env
38     if _jinja_env is None:
39         _jinja_env = create_env()
40     return _jinja_env
41
42
43 def create_env():
44     """Create a new Jinja2 environment."""
45     searchpath = list(settings.JINJA2_TEMPLATE_DIRS)
46     return Environment(loader=FileSystemLoader(searchpath),
47                        auto_reload=settings.TEMPLATE_DEBUG,
48                        cache_size=getattr(settings, 'JINJA2_CACHE_SIZE', 50),
49                        extensions=getattr(settings, 'JINJA2_EXTENSIONS', ()))
50
51
52 def get_template(template_name, globals=None):
53     """Load a template."""
54     try:
55         return get_env().get_template(template_name, globals=globals)
56     except TemplateNotFound, e:
57         raise TemplateDoesNotExist(str(e))
58
59
60 def select_template(templates, globals=None):
61     """Try to load one of the given templates."""
62     env = get_env()
63     for template in templates:
64         try:
65             return env.get_template(template, globals=globals)
66         except TemplateNotFound:
67             continue
68     raise TemplateDoesNotExist(', '.join(templates))
69
70
71 def render_to_string(template_name, context=None, request=None,
72                      processors=None):
73     """Render a template into a string."""
74     context = dict(context or {})
75     if request is not None:
76         context['request'] = request
77         for processor in chain(get_standard_processors(), processors or ()):
78             context.update(processor(request))
79     return get_template(template_name).render(context)
80
81
82 def render_to_response(template_name, context=None, request=None,
83                        processors=None, mimetype=None):
84     """Render a template into a response object."""
85     return HttpResponse(render_to_string(template_name, context, request,
86                                          processors), mimetype=mimetype)