python - How to dynamically add items to jinja variable in Flask? -


i'm building website using flask jinja2 templating engine , i'm dynamically building menu (as described here):

{%     set navigation_bar = [         ('/', 'index', 'home'),         ('/aboutus/', 'aboutus', 'about us'),         ('/faq/', 'faq', 'faq')     ] %}  {% set active_page = active_page|default('index') -%}  <ul> {% href, id, title in navigation_bar %}     <li{% if id == active_page %} class="active"{% endif %}>         <a href="{{ href|e }}">{{ title|e }}</a>     </li> {% endfor %} </ul> 

now if user logged in want show additional things. @ runtime want add items navigation_bar variable. tried this:

{% if g.user.is_authenticated() %}     {% navigation_bar.append(('/someotherpage', 'someotherpage', 'someotherpage')) -%} {% endif %} 

but unfortunately results in following error: templatesyntaxerror: encountered unknown tag 'navigation_bar'. jinja looking following tags: 'endblock'. innermost block needs closed 'block'.

so: know how can add additional items jinja2 variable @ runtime? tips welcome!

[bonus question] wondering, - @ end of {% set active_page = active_page|default('index') -%}?

the error occurs because jinja can't identify block. each jinja block should start block name. do block do extension meets needs. use should add extension jinja extensions. can so:

app.jinja_env.add_extension('jinja2.ext.do') 

and can use extension. example should looks this:

{% if g.user.is_authenticated() %}     {% navigation_bar.append(('/someotherpage', 'someotherpage', 'someotherpage')) %} {% endif %} 

here's simple example.

you find answer bonus question here. in short - removes whitespaces start or end of block (this depends on located).


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -