python - Calling template_filter function in flask only once? -


i have simple filter should print out value passed it. created simple template use it. first time page viewed, value printed in terminal, after template rendered not printed again. why filter being called once?

@app.route('/') def hello_world():     return render_template('test.html')  @app.template_filter() def allow_menu(menu):     print('allow_menu : {}'.format(menu))     return menu == 'test' 
{% if 'test' | allow_menu %}ok{% endif %} 
$ python main.py * running on http://0.0.0.0:9876/ (press ctrl+c quit) allow_menu : test 10.10.2.62 - - [31/aug/2015 15:50:46] "get / http/1.1" 200 - 10.10.2.62 - - [31/aug/2015 15:50:47] "get / http/1.1" 200 -  

the first time template file loaded, jinja compiles , caches result internally. since passing constant 'test' filter, jinja optimizes evaluating during compilation, rather during each render.

the first time template used, filter called. subsequent renders use cached version evaluated constsnt , function not called. if value passed filter not constant, filter evaluated during each render instead.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -