What other ways to put active class to a template in flask?

Learn flask. Make a test site using templates. The site menu in base.html. When you navigate to a specific page the link is active is highlighted, it is given a class of active. Is found the output is:
<li {% if request.path == '/' %}class="active"{% endif %}>
 <a href="/">Home</a>

Is this correct and are there other options how to assign a class active to the active link when using templates?
April 3rd 20 at 17:45
1 answer
April 3rd 20 at 17:47
You can:
<ul class="nav navbar-nav navbar-right">
 <li {{ home_active }}><a href="/">Home</a></li>
 <li {{ places_active }}><a href="/places">Places</a></li> 

def index():
 return render_template('index.html', home_active="class=active")

def map():
 return render_template('places.html', places_active="class=active")

Or this:

def some_view():
 return render_template('template.html', active='home')

<li class="{% if active=='home' %}active{%endif %}">Home</li>
<li class="{% if active=='blog' %}active{%endif %}">Blog</li>

Highlighting Active Menu Items


{% extends "layout.html" %}
{% set active_page = "index" %}

{% 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 %}

Thanks, Your first solution seems the most elegant - Ward.Fahey commented on April 3rd 20 at 17:50
@Ward.Fahey, first exactly inelegant, it is the logic of templates (title of class) brings into view. A right to view about it knew nothing. - Elwi commented on April 3rd 20 at 17:53
@Elwi, explain how, just learn :)
Did the option when the menu is a named list, and a pattern is formed in the cycle menu.
{% set menu = {'/': 'Home', '/doc': 'Documents'} %}

{% for item in menu %}
 <a {% if request.path == item %}class="active"{% endif %} href="{{ item }}">{{ menu[item] }}</a><hr>
{% endfor %}
- Ward.Fahey commented on April 3rd 20 at 17:56

Find more questions by tags FlaskJinja2