Skip to content

Commit 2dd1320

Browse files
committed
templating: proof of concept for Jinja2 support. Select 'jinja2'
template_engine in config and place .html templates into html/jinja2
1 parent 813e974 commit 2dd1320

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Features:
1313
limiting the responsibility of Loaders to compilation and rendering.
1414
Internally, templating.find_template is replaced with
1515
client.selectTemplate (anatoly techtonik)
16+
- Experimental proof of concept code for Jinja2 templating engine.
17+
Select 'jinja2' template_engine in config and place templates into
18+
html/jinja2 to play with (anatoly techtonik)
1619

1720
Fixed:
1821

roundup/cgi/engine_jinja2.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
Experimental Jinja2 support for Roundup. It will become less
3+
experimental when it is completely clear what information is
4+
passed to template, and when the info is limited to the sane
5+
minimal set (to avoid Roundup state changes from template).
6+
7+
[ ] fallback mechanizm to use multiple templating engines in
8+
parallel and aid in incremental translation from one
9+
engine to another
10+
11+
[ ] define a place for templates
12+
probably
13+
TRACKER_HOME/templates/jinja2
14+
with
15+
TRACKER_HOME/templates/INFO.txt
16+
describing how the dir was created, for example
17+
"This is a copy of 'classic' template from ..."
18+
also template fallback mechanizm for multi-engine
19+
configuration
20+
[ ] backward compatibility - if no engine is explicitly
21+
specified, use TRACKER_HOME/html directory
22+
[ ] copy TEMPLATES-INFO.txt to INFO.txt
23+
[ ] implement VERSION file in environment for auto
24+
upgrade
25+
[ ] figure out what to do with autoescaping - it is disabled
26+
by default in Jinja2
27+
28+
[ ] precompileTemplates is a stub
29+
30+
[ ] add {{ debug() }} dumper to inspect available variables
31+
https://github.com/mitsuhiko/jinja2/issues/174
32+
"""
33+
34+
import jinja2
35+
36+
# http://jinja.pocoo.org/docs/api/#loaders
37+
38+
from roundup.cgi.templating import context, LoaderBase, TemplateBase
39+
40+
class Jinja2Loader(LoaderBase):
41+
def __init__(self, dir):
42+
jinjadir = dir + '/jinja2'
43+
# [ ] separate configuration when multiple loaders are used
44+
print "Jinja2 templates:", jinjadir
45+
self._env = jinja2.Environment(
46+
loader=jinja2.FileSystemLoader(jinjadir)
47+
)
48+
49+
def check(self, tplname):
50+
print tplname
51+
try:
52+
print self._env.get_template(tplname + '.html')
53+
except jinja2.TemplateNotFound:
54+
return
55+
else:
56+
return True
57+
58+
def load(self, tplname):
59+
#src, filename = self.check(tplname)
60+
return Jinja2ProxyPageTemplate(self._env.get_template(tplname + '.html'))
61+
62+
def precompileTemplates(self):
63+
pass
64+
65+
class Jinja2ProxyPageTemplate(TemplateBase):
66+
def __init__(self, template):
67+
self._tpl = template
68+
69+
def render(self, client, classname, request, **options):
70+
# [ ] limit the information passed to the minimal necessary set
71+
c = context(client, self, classname, request)
72+
'''c.update({'options': options})
73+
74+
def translate(msgid, domain=None, mapping=None, default=None):
75+
result = client.translator.translate(domain, msgid,
76+
mapping=mapping, default=default)
77+
return unicode(result, client.translator.OUTPUT_ENCODING)
78+
79+
output = self._pt.render(None, translate, **c)
80+
return output.encode(client.charset)
81+
'''
82+
return self._tpl.render(c)
83+
84+
def __getitem__(self, name):
85+
# [ ] figure out what are these for
86+
raise NotImplemented
87+
#return self._pt[name]
88+
89+
def __getattr__(self, name):
90+
# [ ] figure out what are these for
91+
raise NotImplemented
92+
#return getattr(self._pt, name)
93+

roundup/cgi/templating.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ def __str__(self):
7777

7878
class LoaderBase:
7979
""" Base for engine-specific template Loader class."""
80+
def __init__(self, dir):
81+
# loaders are given the template directory as a first argument
82+
pass
83+
8084
def precompileTemplates(self):
8185
""" Go through a directory and precompile all the templates therein
8286
"""
@@ -118,13 +122,17 @@ def __getitem__(self, name):
118122
except NoTemplate, message:
119123
raise KeyError, message
120124

125+
class TemplateBase:
126+
content_type = 'text/html'
121127

122128
def get_loader(dir, engine_name):
123129
if engine_name == 'chameleon':
124-
import engine_chameleon as engine
130+
from engine_chameleon import Loader
131+
elif engine_name == 'jinja2':
132+
from engine_jinja2 import Jinja2Loader as Loader
125133
else:
126-
import engine_zopetal as engine
127-
return engine.Loader(dir)
134+
from engine_zopetal import Loader
135+
return Loader(dir)
128136

129137
def context(client, template=None, classname=None, request=None):
130138
"""Return the rendering context dictionary

0 commit comments

Comments
 (0)