comparison roundup/cgi/engine_jinja2.py @ 4743:2d6959f1d2df

templating: proof of concept for Jinja2 support. Select 'jinja2' template_engine in config and place .html templates into html/jinja2
author anatoly techtonik <techtonik@gmail.com>
date Thu, 17 Jan 2013 15:21:54 +0300
parents
children e339583eae8e
comparison
equal deleted inserted replaced
4742:9cc6d463cfbe 4743:2d6959f1d2df
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 Issue Tracker: http://roundup-tracker.org/