Skip to content

Commit 01c1bed

Browse files
issue2550811 20% fix: jinja2 template engine now has an example how to use non-ascii unicode contents with a custom filter ('|u').
1 parent c862b2a commit 01c1bed

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

CHANGES.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ Fixed:
173173
- Help-Window now gets focus, this prevents the case that help doesn't
174174
work because an old help-window is below the main window.
175175
(Ralf Schlatterbeck)
176+
- issue2550811 20% fix: jinja2 template engine now has an example
177+
how to use non-ascii unicode contents with a custom filter ('|u').
178+
See updates on http://www.roundup-tracker.org/cgi-bin/moin.cgi/Jinja2
179+
(Bernhard Reiter)
176180

177181

178182
2013-07-06: 1.5.0

roundup/cgi/engine_jinja2.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import jinja2
3535
import gettext
3636

37+
from types import MethodType
38+
3739
# http://jinja.pocoo.org/docs/api/#loaders
3840

3941
from roundup.cgi.templating import context, LoaderBase, TemplateBase
@@ -43,13 +45,22 @@ def __init__(self, dir):
4345
extensions = [
4446
'jinja2.ext.autoescape',
4547
]
46-
print "Jinja2 templates: ", dir
48+
print "Jinja2 templates: ", dir
4749
print "Extensions: ", extensions
4850
self._env = jinja2.Environment(
4951
loader=jinja2.FileSystemLoader(dir),
5052
extensions=extensions
5153
)
5254

55+
# Adding a custom filter that can transform roundup's vars to unicode
56+
# This is necessary because jinja2 can only deal with unicode objects
57+
# and roundup uses utf-8 for the internal representation.
58+
# The automatic conversion will assume 'ascii' and fail sometime.
59+
# Analysed with roundup 1.5.0 and jinja 2.7.1. See issue2550811.
60+
self._env.filters["u"] = lambda s: \
61+
unicode(s(), "utf-8") if type(s) == MethodType \
62+
else unicode(s, "utf-8")
63+
5364
def check(self, tplname):
5465
#print tplname
5566
try:

share/roundup/templates/jinja2/html/issue.item.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ <h4>Messages</h4>
6464
</div>
6565
</div>
6666
<div class='row-fluid'>
67-
<pre>{{ msg.content.hyperlinked() }}</pre>
67+
<pre>{{ msg.content.hyperlinked()|u }}</pre>
6868
</div>
6969
{% endfor %}
7070
{% endif %}

share/roundup/templates/jinja2/html/msg.item.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
<div class='row-fluid'>
3838
<h4>Content</h4>
39-
<pre>{{ context.content.hyperlinked }}</pre>
39+
<pre>{{ context.content.hyperlinked|u }}</pre>
4040
</div>
4141
{% endif %}
4242

0 commit comments

Comments
 (0)