[Flask] Jinja environment finalize function

S McGraw smcg4191 at mtneva.com
Tue Oct 6 05:40:56 CEST 2015

On 10/02/2015 05:16 AM, David Nieder wrote:
> On 01.10.2015 07:15, S McGraw wrote:
>> Jinja allows one to initialize an environment with a
>> "finalize" callable, which I want to do to convert None's
>> to empty strings before they are output.  This is much
>> cleaner than than filling my templates with innumerable
>> "if none ..." constructs.
>> But I don't see in the Flask docs how to do this.
>> Suggestions?
> Dont't know if this is the way to go but just patching the jinja
> environment works for me:
> def finalize(arg):
>     if arg is None:
>         return ''
>     return arg
> app = Flask(__name__)
> app.jinja_env.finalize = finalize

Yup, that worked fine.  I should have tried that before posting 
but I thought I remembered trying to do that (set the finalize 
value after the environment was initialized) a while ago in a 
non-Flask project, unsuccessfully.  Or maybe Flask has not set 
the environment yet, I didn't look at the Flask code.  In any 
event, thanks!

More information about the Flask mailing list