None and the unicode-filtering

224 views
Skip to first unread message

Martin

unread,
Jul 26, 2008, 10:58:39 AM7/26/08
to Mako Templates for Python
Hi all,

When I have a None-value in a variable, then Mako by default seems to
render this as 'None'. I saw the other thread some months ago (the
Google-Groups web interface didn't let my reply, so I had to open a
new one... sorry). So, apparently the 'None' is rendered as string
because the unicode-filter is the default.

Is there a convenient way to have None rendered as empty string? For
example, I have a form where the default value of a text field coming
from the controller might be None... so I simply want the text field
to be empty in that case. It would work to switch of the default
filter with
n
and then append my own filter (but slightly unpractial, I think). Is
there an easier way to prevent None from being output as text?

Michael Bayer

unread,
Jul 26, 2008, 11:32:23 AM7/26/08
to Mako Templates for Python
Personally when something might be "None" I usually write the
expression as ${val or ''}. But, to do it automatically, you'd have
to replace the default "unicode" filter with a function of your own
that checks for None.

Roger Demetrescu

unread,
Jul 26, 2008, 1:00:20 PM7/26/08
to mako-d...@googlegroups.com

If you want the rule "convert None to empty string" to be applied for
your entires
templates, you can do it:


lookup = TemplateLookup(...,
default_filters=["none_to_blank", "unicode"],
imports=['from utils import none_to_blank'])


and in your utils.py:


def none_to_blank(value):
if value is None:
return ""
else:
return value


Cheers,

Roger

Martin

unread,
Jul 28, 2008, 5:01:59 PM7/28/08
to Mako Templates for Python
I like both of these (nice and simple). Not sure which one I'll use,
but anyway: Thanks!

Cheers,
Martin
Reply all
Reply to author
Forward
0 new messages