Omitting Unicode prefix from inside template

29 views
Skip to first unread message

Christian Wutte

unread,
Feb 7, 2012, 5:49:41 PM2/7/12
to Mako Templates for Python
Hello there,
I am using a program which relies on Mako. My problem is that some
type of strings (HTML escaped ones) are always prefixed with u. So my
question is, if there is anything supported to omit this from inside
the file template? I don't have access to the calling python file.

Thank you,
Christian

Michael Bayer

unread,
Feb 7, 2012, 6:08:20 PM2/7/12
to mako-d...@googlegroups.com

I'm not entirely clear what you're describing, usually a template doesn't have very many literal Python strings at all. Such literals are usually coming from the calling application. Within the template, you wouldn't usually be saying "${u'some string'}", you'd be saying, "some string".

If you are actually doing something like ${u'some string'}, if 'some string' is all ASCII then you can omit the 'u'.


Christian Wutte

unread,
Feb 7, 2012, 6:55:31 PM2/7/12
to Mako Templates for Python
Sorry, I should have mentioned that I mean the resulting file. The
file that is generated using Mako templates has the prefixes.
So something like ${certainApi()} is resulting in an string like
u'some data'.

Michael Bayer

unread,
Feb 7, 2012, 7:32:47 PM2/7/12
to mako-d...@googlegroups.com
the module file ? I can't really see how that's possible, can you provide a short example ?

> --
> You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
> To post to this group, send email to mako-d...@googlegroups.com.
> To unsubscribe from this group, send email to mako-discuss...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mako-discuss?hl=en.
>

Christian Wutte

unread,
Feb 8, 2012, 5:29:02 AM2/8/12
to Mako Templates for Python
I wrongly thought of it as the result file of mako, but in fact it is
the applications result file. So I meant something like the string
from template.render() written to a file (there are no module files
used). In that file some parts are always surrounded with u''.

Michael Bayer

unread,
Feb 8, 2012, 10:20:16 AM2/8/12
to mako-d...@googlegroups.com
Whatever the output of your template is, is a result of what you placed into that template, and what kinds of functions you're using to render content. There's nothing in Mako that would render the literal u'' in the output directly, so again a short example would be most helpful here.

Christian Wutte

unread,
Feb 11, 2012, 9:10:56 AM2/11/12
to Mako Templates for Python
An example would be just:
Mako template: ${apiCall1()}, $(apiCall2()}
resulting in an
output file: Text1, u'Text2'

The function apiCall2() always returns an Unicode string. I have
neither control over the rendering settings nor the apiCall2()
internals.

So with my original post I thought of some Mako settings to omit the u-
prefix, but in fact it is a Python question.
Possibly an context.write(apiCall2()) in the template could change
something? I will try that.
Otherwise if I understand it correctly the application (where the
template rendering is done) should be responsible for the correct
output.

Thanks for your help!

Michael Bayer

unread,
Feb 11, 2012, 10:08:44 AM2/11/12
to mako-d...@googlegroups.com

On Feb 11, 2012, at 9:10 AM, Christian Wutte wrote:

> An example would be just:
> Mako template: ${apiCall1()}, $(apiCall2()}
> resulting in an
> output file: Text1, u'Text2'

then that's coming from your apiCall2() function.


>
> The function apiCall2() always returns an Unicode string.

If that is exactly what you see, then this is not the full story. It's returning a unicode string that *contains* the characters "u''", such as:

"u'Text2'"

Here is an example:

from mako.template import Template
t = Template("""
${apiCall1()}, ${apiCall2()}
""")

def apiCall1():
return "Text1"

def apiCall2():
return "u'Text2'"

print t.render(apiCall1=apiCall1, apiCall2=apiCall2)

that's the only way to get that exact effect. Or if apiCall2() returns an object with a __str__() method that resolves to it's repr(). The issue lies within apiCall2() likely calling repr() on a string value instead of str().


> I have
> neither control over the rendering settings nor the apiCall2()
> internals.

It would appear apiCall2's output is pretty wrong, but you'd need to filter apiCall2's output:

from mako.template import Template
import re

t = Template("""
${apiCall1()}, ${apiCall2() | unRepr}
""")

def apiCall1():
return "Text1"

def apiCall2():
return "u'Text2'"

def unRepr(value):
return re.sub(r"u'(.*?)'", lambda m:m.group(1), value)

print t.render(apiCall1=apiCall1, apiCall2=apiCall2, unRepr=unRepr)

Christian Wutte

unread,
Feb 22, 2012, 1:36:55 PM2/22/12
to Mako Templates for Python
That explains my unsuccessful attempts getting rid of it ,)
Thanks!
Reply all
Reply to author
Forward
0 new messages