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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 | diff -r 576595d1d485 pylons/templates/default_project/+package+/config/environment.py_tmpl
--- a/pylons/templates/default_project/+package+/config/environment.py_tmpl Thu Apr 24 12:16:56 2008 -0700
+++ b/pylons/templates/default_project/+package+/config/environment.py_tmpl Thu Apr 24 13:39:40 2008 -0700
@@ -6,6 +6,8 @@
from mako.lookup import TemplateLookup
{{elif template_engine == 'genshi'}}
from genshi.template import TemplateLoader
+{{elif template_engine == 'jinja'}}
+from jinja import ChoiceLoader, Environment, FileSystemLoader
{{endif}}
from pylons import config
{{if sqlalchemy}}
@@ -50,6 +52,13 @@
# Create the Genshi TemplateLoader
config['pylons.app_globals'].genshi_loader = TemplateLoader(
paths['templates'], auto_reload=True)
+ {{elif template_engine == 'jinja'}}
+
+ # Create the Jinja Environment
+ config['pylons.app_globals'].jinja_env = Environment(loader=ChoiceLoader(
+ [FileSystemLoader(path) for path in paths['templates']]))
+ # Jinja's unable to request c's attributes without strict_c
+ config['pylons.strict_c'] = True
{{endif}}
{{if sqlalchemy}}
# Setup SQLAlchemy database engine
diff -r 576595d1d485 pylons/templates/default_project/+package+/lib/base.py_tmpl
--- a/pylons/templates/default_project/+package+/lib/base.py_tmpl Thu Apr 24 12:16:56 2008 -0700
+++ b/pylons/templates/default_project/+package+/lib/base.py_tmpl Thu Apr 24 13:39:40 2008 -0700
@@ -3,7 +3,7 @@
Provides the BaseController class for subclassing.
"""
from pylons.controllers import WSGIController
-{{if template_engine in ('genshi', 'mako')}}
+{{if template_engine in ('genshi', 'jinja', 'mako')}}
from pylons.templating import render_{{template_engine}} as render
{{endif}}
{{if sqlalchemy}}
diff -r 576595d1d485 pylons/templates/minimal_project/+package+/controllers/__init__.py_tmpl
--- a/pylons/templates/minimal_project/+package+/controllers/__init__.py_tmpl Thu Apr 24 12:16:56 2008 -0700
+++ b/pylons/templates/minimal_project/+package+/controllers/__init__.py_tmpl Thu Apr 24 13:39:40 2008 -0700
@@ -3,6 +3,9 @@
Provides the BaseController class for subclassing.
"""
from pylons.controllers import WSGIController
+{{if template_engine in ('genshi', 'jinja', 'mako')}}
+from pylons.templating import render_{{template_engine}} as render
+{{endif}}
class BaseController(WSGIController):
diff -r 576595d1d485 pylons/templates/minimal_project/+package+/wsgiapp.py_tmpl
--- a/pylons/templates/minimal_project/+package+/wsgiapp.py_tmpl Thu Apr 24 12:16:56 2008 -0700
+++ b/pylons/templates/minimal_project/+package+/wsgiapp.py_tmpl Thu Apr 24 13:39:40 2008 -0700
@@ -6,6 +6,8 @@
from mako.lookup import TemplateLookup
{{elif template_engine == 'genshi'}}
from genshi.template import TemplateLoader
+{{elif template_engine == 'jinja'}}
+from jinja import ChoiceLoader, Environment, FileSystemLoader
{{endif}}
from paste.cascade import Cascade
from paste.registry import RegistryManager
@@ -52,6 +54,13 @@
# Create the Genshi TemplateLoader
config['pylons.app_globals'].genshi_loader = TemplateLoader(
paths['templates'], auto_reload=True)
+ {{elif template_engine == 'jinja'}}
+
+ # Create the Jinja Environment
+ config['pylons.app_globals'].jinja_env = Environment(loader=ChoiceLoader(
+ [FileSystemLoader(path) for path in paths['templates']]))
+ # Jinja's unable to request c's attributes without strict_c
+ config['pylons.strict_c'] = True
{{endif}}
# CONFIGURATION OPTIONS HERE (note: all config options will override
diff -r 576595d1d485 pylons/templating.py
--- a/pylons/templating.py Thu Apr 24 12:16:56 2008 -0700
+++ b/pylons/templating.py Thu Apr 24 13:39:40 2008 -0700
@@ -4,10 +4,11 @@
============================
:mod:`pylons.templating` includes several basic render functions,
-:func:`render_mako` and :func:`render_genshi` that render templates
-from the file-system with the assumption that variables intended for
-the will be attached to :data:`tmpl_context` (hereafter referred to
-by its short name of :data:`c` which it is commonly imported as).
+:func:`render_mako`, :func:`render_genshi` and :func:`render_jinja` that
+render templates from the file-system with the assumption that variables
+intended for the will be attached to :data:`tmpl_context` (hereafter
+referred to by its short name of :data:`c` which it is commonly imported
+as).
The default render functions work with the template language loader
object that is setup on the :data:`app_globals` object in the project's
@@ -57,9 +58,9 @@
----------------
Templates rendered in Pylons should include the default Pylons globals
-as the :func:`render_mako` and :func:`render_genshi` functions. The
-full list of Pylons globals that are included in the template's
-namespace are:
+as the :func:`render_mako`, :func:`render_genshi` and
+:func:`render_jinja` functions. The full list of Pylons globals that are
+included in the template's namespace are:
- :term:`c` -- Template context object
- :term:`tmpl_context` -- Template context object
@@ -177,7 +178,7 @@
import pylons
__all__ = ['Buffet', 'MyghtyTemplatePlugin', 'render', 'render_genshi',
- 'render_mako', 'render_response']
+ 'render_jinja', 'render_mako', 'render_response']
PYLONS_VARS = ['c', 'config', 'g', 'h', 'render', 'request', 'session',
'translator', 'ungettext', '_', 'N_']
@@ -301,7 +302,7 @@
return template.render(**globs)
- return cached_template(template_name, render_template, cache_key=cache_key,
+ return cached_template(template_name, render_template, cache_key=cache_key,
cache_type=cache_type, cache_expire=cache_expire)
@@ -324,9 +325,33 @@
return template.generate(**globs).render(method=method)
- return cached_template(template_name, render_template, cache_key=cache_key,
+ return cached_template(template_name, render_template, cache_key=cache_key,
cache_type=cache_type, cache_expire=cache_expire,
ns_options=('method'), method=method)
+
+
+def render_jinja(template_name, cache_key=None, cache_type=None,
+ cache_expire=None):
+ """Render a template with Jinja
+
+ Accepts the cache options ``cache_key``, ``cache_type``, and
+ ``cache_expire``.
+
+ """
+ # Create a render callable for the cache function
+ def render_template():
+ # First, get the globals
+ globs = pylons_globals()
+
+ # Grab a template reference
+ template = \
+ globs['app_globals'].jinja_env.get_template(template_name)
+
+ return template.render(**globs)
+
+ return cached_template(template_name, render_template, cache_key=cache_key,
+ cache_type=cache_type, cache_expire=cache_expire)
+
class BuffetError(Exception):
"""Buffet Exception"""
diff -r 576595d1d485 pylons/util.py
--- a/pylons/util.py Thu Apr 24 12:16:56 2008 -0700
+++ b/pylons/util.py Thu Apr 24 13:39:40 2008 -0700
@@ -149,7 +149,7 @@
summary = 'Pylons application template'
egg_plugins = ['PasteScript', 'Pylons']
vars = [
- var('template_engine', 'mako/genshi/etc: Template language',
+ var('template_engine', 'mako/genshi/jinja/etc: Template language',
default='mako'),
var('sqlalchemy', 'True/False: Include SQLAlchemy 0.4 configuration',
default=False),
@@ -182,7 +182,7 @@
_template_dir = 'templates/minimal_project'
summary = 'Pylons minimal application template'
vars = [
- var('template_engine', 'mako/genshi/etc: Template language',
+ var('template_engine', 'mako/genshi/jinja/etc: Template language',
default='mako'),
]
|