2

I have a base template named main.html:

<ul>
  <li>index</li>
  <li>about</li>
  <li>contacts</li>
</ul>

And I have a template index.html, which has:

{% extends "main.html" %}

How I can add class atributes into <li> tags depending on named heir?

For example, if index.html extends main.html, then I add class="active" to first <li>, if about.html extends main.html, then I add class="active" to second <li> .... and so on.

How I can do it?

davidism
  • 121,510
  • 29
  • 395
  • 339
Nolik
  • 4,337
  • 5
  • 18
  • 14

3 Answers3

1

One way could be to keep the main.html as is and override it at the child level (about.html etc) by calling super ? I have not tested this code but something like:

main.html

{% block menu_bar %}
<ul>
  <li>index</li>
  <li>about</li>
  <li>contacts</li>
</ul>
{% endblock %}

about.html

{% block menu_bar %}
<li class="active">about</li>
{{ super() }}
{% endblock %}
codegeek
  • 32,236
  • 12
  • 63
  • 63
0

there are kinda two solution to this:

the first way is defining a macro and calling it from child pages(not the inherited main page) which contain information to make which <li> active.

like :

{% macro menu(active) %}

<ul>
  {% if active == 'index' %}<li class="active">{% else %}<li>{%endif%}index</li>
  {% if active == 'about' %}<li class="active">{% else %}<li>{%endif%}about</li>
  {% if active == 'contacts' %}<li class="active">{% else %}<li>{%endif%}contacts</li>
</ul>

{% endmacro %}

and use it as :

{% from 'macro.html' import menu %}

{{ macro('index') }} #in index.html
{{ macro('about') }} #in about.html
{{ macro('contacts') }} #in contacts.html

another ways is using the magical g variable. in your view functions define which item should be the active one and put in the g variable. like:

from flask import g

app.route('/about')
def about():
  ...
  g.active_menu_item = 'about'

  ...

  return render_template('about.html')

and your about.html(index and contacts too) inherits from main.html', so the codes that render menu ofmain.htmlshould considerg.active_menu_item` into account. like:

main.html :

<ul>
  {% if g.active_menu_item == 'index' %}<li class="active">{% else %}<li>{%endif%}index</li>
  {% if g.active_menu_item == 'about' %}<li class="active">{% else %}<li>{%endif%}about</li>
  {% if g.active_menu_item == 'contacts' %}<li class="active">{% else %}<li>{%endif%}contacts</li>
</ul>
thkang
  • 11,215
  • 14
  • 67
  • 83
0

menu.html

<ul>
  <li class="{% if active_menu == 'index' %}active{% endif %}">index</li>
  <li class="{% if active_menu == 'about' %}active{% endif %}">about</li>
  <li class="{% if active_menu == 'contacts' %}active{% endif %}"contacts</li>
</ul>

index.html

{% extends "main.html" %}

{% include "menu.html" with active_menu='index' %}

about.html

{% extends "main.html" %}

{% include "menu.html" with active_menu='about' %}

contacts.html

{% extends "main.html" %}

{% include "menu.html" with active_menu='contacts' %}
catherine
  • 22,492
  • 12
  • 61
  • 85