I got the following html snippet from my page template.html
.
<ul class='nav'> <li class="active"><a href='/'>Home</a></li> <li><a href='/lorem'>Lorem</a></li> {% if session['logged_in'] %} <li><a href="/account">Account</a></li> <li><a href="/projects">Projects</a> <li><a href="/logout">Logout</a></li> {% endif %} {% if not session['logged_in'] %} <li><a href="/login">Login</a></li> <li><a href="/register">Register</a></li> {% endif %} </ul>
As you can see on line 2, there's the class active. This highlights the active tab with the twitter bootstrap css file. Now, this will work fine if I would visit www.page.com/
but not when I would visit www.page.com/login
for example. It would still highlight the home link as the active tab.
Of course, I could easily do this with Javascript/jQuery but I'd rather not use that in this situation.
There's already a working solution for ruby on rails but I don't know how to convert that into python/jinja (I'm rather new to jinja/flask, never worked with ruby at all)
To set an active class in your bootstrap navbar, you can use ng-controller(NavigationController) to set bootstrap navbar active class with AngularJS. To run a single controller outside ng-view. You can set class= “active” when the angular route is clicked.
You can install Flask's Bootstrap dependency with pip install flask-bootstrap in your terminal. However, using flask-bootstrap can limit the control you have over the styling and design of your website. It also has documentation here, and for the documentation specific to flask-bootstrap, go here.
Class="active" is usually used to highlight any active content from a selection, mostly in navigation.
You can customize links using the active class to indicate the current page.
Have you looked at this ? https://jinja.palletsprojects.com/en/3.0.x/tricks/#highlighting-active-menu-items
Often you want to have a navigation bar with an active navigation item. This is really simple to achieve. Because assignments outside of blocks in child templates are global and executed before the layout template is evaluated it’s possible to define the active menu item in the child template:
{% extends "layout.html" %} {% set active_page = "index" %}
The layout template can then access active_page
. Additionally it makes sense to define a default for that variable:
{% set navigation_bar = [ ('/', 'index', 'Index'), ('/downloads/', 'downloads', 'Downloads'), ('/about/', 'about', 'About') ] -%} {% set active_page = active_page|default('index') -%} ... <ul id="navigation"> {% for href, id, caption in navigation_bar %} <li{% if id == active_page %} class="active"{% endif %}><a href="{{ href|e }}">{{ caption|e }}</a> </li> {% endfor %} </ul>
Here is another simpler way if you have menus distributed all over the page. This way uses inline if statements to print out the class active.
<ul> <li class="{{ 'active' if active_page == 'menu1' else '' }}"> <a href="/blah1">Link 1</a> </li> <li class="{{ 'active' if active_page == 'menu2' else '' }}"> <a href="/blah2"> Link 2 </a> </li> </ul>
Class active is for highlighting
You still need to set the variable on every page to mark them
{% extends "master.html" %} {% set active_page = "menu1" %}
or
{% set active_page = "menu2" %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With