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)
Have you looked at this ? http://jinja.pocoo.org/docs/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:
The layout template can then access
active_page
. Additionally it makes sense to define a default for that variable: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.
You still need to set the variable on every page to mark them
or
I liked @philmaweb's approach, but there's really no reason to require duplicating the endpoint in the id of each element.
base.js:
base.html
Why not just put this script inline? You could, of course, but allowing inline JS is a security nightmare. You should be using a CSP on your site (e.g. Flask-Talisman) which will not allow inline JS. With
data-*
attributes, it's not hard to do this in a secure way.NB: If you have multiple links leading to the same, current page and you want only ONE of them to be marked "active"—then this approach may not work for you.
For jinja/flask/bootstrap users:
If you define your nav like they did in the blog example http://getbootstrap.com/examples/blog/ simply assign ids to your links that match your url_for arguments and you just need to modify the layout-template, the rest just works #magic.
At the bottom of your base/layout template just add this
and the right elements will be set active.
EDIT: If you have a layout with elements in a list, like this:
use the parent() function to get the li element instead of the link.
Add the following CSS somewhere on your page:
a[href $= {{ page_name|default("'/'"|safe) }}]{ [INSERT YOUR ACTIVE STYLING HERE] }
Now, on each template define
page_name
, for example:{% extends "template.html" %} {% set page_name = "gallery" %}
This seems much simpler and easier to build on, than other options.
EDIT:
Almost 1 year later I'm returning to make this a much simpler fix, because setting the page name on every page is pretty inefficient.
Instead create a function like so:
This will allow you to pass variables implicitly to jinja, in this case we are passing the request for access to
request.url_rule
which contains the route the user is accessing. In the previous version, we just change{{ page_name|default("'/'"|safe) }}
to"{{ request.url_rule|safe }}"
. Much cleaner.