Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

styling active element menu in flask

Tags:

flask

i have following templates structure in flask:

templates/
  /posts
    list.html
  base.html
  index.html

my base.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ITV</title>
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/main.css') }}">

    <!-- Latest compiled and minified JavaScript -->
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
</head>

<body>

{%- block topbar -%}
    <nav class="navbar navbar-default navbar-static-top" role="navigation">
      <!-- We use the fluid option here to avoid overriding the fixed width of a normal container within the narrow content columns. -->
      <div class="container-fluid">
        <div class="navbar-header">
          <a class="navbar-brand" href="/">САПР</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-8">
          <ul class="nav navbar-nav">
            <li class="active"><a href="/">Головна</a></li>
            <li><a href="/posts">Новини</a></li>
            <li><a href="/schedue">Розклад занять</a></li>
          </ul>
        </div><!-- /.navbar-collapse -->
      </div>
    </nav>
{%- endblock -%}

<div class="container">
    <div class="content">
        {% block page_header %}{% endblock %}
        {% block content %}{% endblock %}
    </div>
</div>
</body>
</html>

i can render template, for example : render_template('posts/list.html'), which extends my base.html

my list.html:

{% extends "base.html" %}

{% block content %}

    Posts
{% endblock %}

How can i set active element menu in base.html

        <li class="active"><a href="/">Головна</a></li>
        <li><a href="/posts">Новини</a></li>
        <li><a href="/schedue">Розклад занять</a></li>

when i'm rendering list.html, and cant pass data directly into base.html?

like image 650
vromanch Avatar asked Dec 07 '22 03:12

vromanch


1 Answers

In Flask, the request variable is available by default in templates, so you can simply check request.path in your base.html and adjust your links.

<li {% if request.path == '/' %}class="active"{% endif %}>
     <a href="/">Головна</a>
</li>
like image 141
Burhan Khalid Avatar answered Jan 02 '23 04:01

Burhan Khalid