Jinja - Is there any built-in variable to get current HTML page name?

后端 未结 2 1682
迷失自我
迷失自我 2020-12-24 07:56

i\'m very new to Jinja and Flask

I want to set different background color in the navigation bar to indicate the current page.

Is there any built-in Jinja var

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-24 08:30

    There is a trick in jinja2 document for your problem: http://jinja.pocoo.org/docs/tricks/

    If your list is simple enough, just using request object, something like that:

  • {{item.text}}
  • Normally, I write this snippet to a macro with an explicit argument to set active:

    {% macro render_sitem(endpoint, display, cls='', icon-cls='', active='') %}
    
  • {{display}}
  • {% endmacro %}

    The list will be something like:

     
    

    So if you have a child page which extends or includes your list, you can set active item like:

    {% set active_page = 'page.index' %} 
    

    in the top of your child page.

提交回复
热议问题