Style active navigation element with a Flask/Jinja2 macro

后端 未结 3 2162
无人及你
无人及你 2020-12-11 09:02

I am using Flask/Jinja2 and Bootstrap 3.

I\'d like to add class=\"active\" to the current navigation element.

Those elements are stored in

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 09:43

    Your code just defines a macro over and over again, it doesn't render anything. Avoid reading request.endpoint and use base templates to do this.

    base.html

    
    
    {% block content %}{% endblock %}
    

    there.html

    {% extends "base.html" %}
    {% block nav_there %}active{% endblock %}
    {% block content %}
        
    No matter where you go, there you are.
    {% endblock %}

    The base navigation defines empty nav_ blocks in the li class and the sub template sets the relevant one to active. You can extend this as far as you want to have sub-navigation within pages too.

提交回复
热议问题