Skip to content

Commit fcb896c

Browse files
committed
fixed issue2550967: support .xml files in addition to .html in the
jinja2 loader
1 parent 838c7b6 commit fcb896c

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ Features:
2424
instead of pyme. (Christof Meerwald)
2525
- issue2550987: Use updated MySQL client module that supports Python
2626
3. (Christof Meerwald)
27+
- issue2550967: the jinja2 loader has been extended to look for .xml
28+
files as well as .html files similar to the TAL loader. (Christof
29+
Meerwald)
2730

2831
Fixed:
2932

roundup/cgi/engine_jinja2.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,48 +34,46 @@
3434
from __future__ import print_function
3535
import jinja2
3636
import gettext
37+
import mimetypes
3738
import sys
3839

39-
from types import MethodType
40-
4140
# http://jinja.pocoo.org/docs/api/#loaders
4241

4342
from roundup.cgi.templating import context, LoaderBase, TemplateBase
4443
from roundup.anypy.strings import s2u
4544

4645
class Jinja2Loader(LoaderBase):
4746
def __init__(self, dir):
48-
extensions = [
49-
'jinja2.ext.autoescape',
50-
]
51-
print("Jinja2 templates: ", dir)
52-
print("Extensions: ", extensions)
5347
self._env = jinja2.Environment(
54-
loader=jinja2.FileSystemLoader(dir),
55-
extensions=extensions
56-
)
48+
loader=jinja2.FileSystemLoader(dir),
49+
extensions=[]
50+
)
5751

5852
# Adding a custom filter that can transform roundup's vars to unicode
5953
# This is necessary because jinja2 can only deal with unicode objects
6054
# and roundup uses utf-8 for the internal representation.
6155
# The automatic conversion will assume 'ascii' and fail sometime.
6256
# Analysed with roundup 1.5.0 and jinja 2.7.1. See issue2550811.
63-
self._env.filters["u"] = lambda s: \
64-
s2u(s()) if type(s) == MethodType else s2u(s)
57+
self._env.filters["u"] = s2u
58+
59+
def _find(self, tplname):
60+
for extension in ('', '.html', '.xml'):
61+
try:
62+
filename = tplname + extension
63+
return self._env.get_template(filename)
64+
except jinja2.TemplateNotFound:
65+
continue
66+
67+
return None
6568

6669
def check(self, tplname):
67-
#print tplname
68-
try:
69-
#print self._env.get_template(tplname + '.html')
70-
self._env.get_template(tplname + '.html')
71-
except jinja2.TemplateNotFound:
72-
return
73-
else:
74-
return True
70+
return bool(self._find(tplname))
7571

7672
def load(self, tplname):
77-
#src, filename = self.check(tplname)
78-
return Jinja2ProxyPageTemplate(self._env.get_template(tplname + '.html'))
73+
tpl = self._find(tplname)
74+
pt = Jinja2ProxyPageTemplate(tpl)
75+
pt.content_type = mimetypes.guess_type(tpl.filename)[0] or 'text/html'
76+
return pt
7977

8078
def precompile(self):
8179
pass

0 commit comments

Comments
 (0)