I have an issue with converting an app from Mako to Jinja. If I have a
piece of Unicode text with, for example, an acute accented é (U+00E9), I
can verify the data is of type unicode (it's coming from a database) and
the string is printed as \xe9. However, when I pass it to
Template.render, the é is output as \xe9, i.e, not converted to utf-8.
In Mako, the Template class has an output_encoding='utf-8' argument,
which did the conversion to utf-8. I can't seem to find anything to do
the conversion in jinja2.
Jinja example:
Python 2.7.2+ (default, Aug 16 2011, 07:03:08)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> msg = u"No lo sé"
>>> src = u"En español: {{ text }}"
>>> msg
u'No lo s\xe9'
>>> src
u'En espa\xf1ol: {{ text }}'
>>> from jinja2 import Template
>>> jt = Template(src)
>>> jt.render(text=msg)
u'En espa\xf1ol: No lo s\xe9'
Mako example (continuing from above):
>>> from mako.template import Template as MTemplate
>>> src = u"En español: ${text}"
>>> src
u'En espa\xf1ol: ${text}'
>>> mt = MTemplate(src)
>>> mt.render(text=msg) # no conversion, as in Jinja
u'En espa\xf1ol: No lo s\xe9'
>>> mt = MTemplate(src, output_encoding='utf-8')
>>> mt.render(text=msg)
'En espa\xc3\xb1ol: No lo s\xc3\xa9'
I see I can do the encoding myself, as suggested in the Mako
documentation, e.g.,
>>> jt.render(text=msg).encode('utf-8', 'replace')
'En espa\xc3\xb1ol: No lo s\xc3\xa9'
I'm a bit surprised because the Jinja2 documentation gave me the
impression that utf-8 was the default encoding, both for the templates
and the output (although I could only find confirmation that it's only
the templates that are assumed to be that way).
So, does the output encoding have to be done manually as above, or did I
miss something?
Joe
--
You received this message because you are subscribed to the Google Groups
"pocoo-libs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pocoo-libs?hl=en.