multiple escape filters for mako

42 views
Skip to first unread message

Jason

unread,
Jun 9, 2011, 1:49:23 PM6/9/11
to pylons-...@googlegroups.com
Hello,
Using Pyramid I would like to filter expression that evaluate to None to be output as an empty string instead of "None". I have made a filter:
def none_escape(value):
    """ Returns empty string if value is None - used as Mako filter. """
    return value if value is not None else ''

and added the imports line to development.ini:
mako.imports = from sales.lib.helpers import none_escape

but when I add the filter to the default filters like this (because I still want the default html filter as well): 
mako.default_filters = h, none_escape

I get the error:
NameError: global name 'h' is not defined

This only happens when I add ", none_escape" to the default filters line. It works with just "h" and the above imports line.

Is this a bug I should be submitting, or am I missing something trivial here?

Thanks,

Jason

Jason

unread,
Jun 9, 2011, 2:01:57 PM6/9/11
to pylons-...@googlegroups.com
Looks like it was something trivial. I needed to format the default filters line as:

mako.default_filters =
    none_escape
    h

And they are required to be in that order. If i put them as:

mako.default_filters =
    h
    none_escape

Then none_escape was not used. In the working order I confirmed both the "h" filter and my none_escape were being used.

--
Jason

Mike Orr

unread,
Jun 9, 2011, 2:03:02 PM6/9/11
to pylons-...@googlegroups.com
On Thu, Jun 9, 2011 at 10:49 AM, Jason <ja...@deadtreepages.com> wrote:
> Hello,
> Using Pyramid I would like to filter expression that evaluate to None to be
> output as an empty string instead of "None". I have made a filter:
> def none_escape(value):
>     """ Returns empty string if value is None - used as Mako filter. """
>     return value if value is not None else ''

I thought we were going to patch Pyramid to do this by default ?

--
Mike Orr <slugg...@gmail.com>

Jason

unread,
Jun 9, 2011, 3:12:25 PM6/9/11
to pylons-...@googlegroups.com
I believe it is patched to use the html filter by default, but not escape None to an empty string. Since I am overwriting the default_filters setting I needed to speciy that I want "h" filter and my own filter.

--
Jason
Reply all
Reply to author
Forward
0 new messages