jinja2 None gets printed

4,030 views
Skip to first unread message

kevin

unread,
Sep 18, 2010, 8:21:06 PM9/18/10
to pocoo...@googlegroups.com
if a variable is None, the text 'None' gets output/printed in the html. is possible to have empty string instead of 'None'?
thanks

Armin Ronacher

unread,
Sep 19, 2010, 12:30:18 PM9/19/10
to pocoo...@googlegroups.com
Hi,

Yes, you can override the finalize method or patch it on the environment:


def silent_none(value):
if value is None:
return ''
return value

from jinja2 import Environment
env = Environment()
env.finalize = silent_none


I however recommend being explicit instead of a finalizer:

{{ variable or '' }}


Regards,
Armin

kevin

unread,
Sep 19, 2010, 3:04:18 PM9/19/10
to pocoo...@googlegroups.com
On Sun, Sep 19, 2010 at 9:30 AM, Armin Ronacher <armin.r...@active-4.com> wrote:
Hi,


On 2010-09-19 2:21 AM, kevin wrote:
if a variable is None, the text 'None' gets output/printed in the html.
is possible to have empty string instead of 'None'?
thanks
Yes, you can override the finalize method or patch it on the environment:


def silent_none(value):
   if value is None:
       return ''
   return value

from jinja2 import Environment
env = Environment()
env.finalize = silent_none
thanks for the info
 
I however recommend being explicit instead of a finalizer:

 {{ variable or '' }}
this seems easier and better, i did not know i could do this in jinja2
jinja2 rocks, thanks
Reply all
Reply to author
Forward
0 new messages