S
S
Sergey Ilyin2020-05-28 03:08:04
css
Sergey Ilyin, 2020-05-28 03:08:04

How to reuse navigation div on all pages?

There is a navigation block on the site:

<nav class="navigation block">
        <a class="nav-link active" href="/">Главная</a>
        <a class="nav-link" href="/xxx">xxx</a>
        <a class="nav-link" href="/yyy">yyy</a>
        <a class="nav-link" href="/zzz">zzz</a>
    </nav>

Fixed:
how to correctly inherit the template in Flask so that when visiting each page from the navigation menu, the selected block becomes active? if you inherit what is now - nav-link active is saved for /index.html, but I would like it all to live somehow dynamically.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arseny, 2020-05-29
@sunsexsurf

Well, in general, Roman answered in the comments on the case, except that I would recommend checking the substring, since request.path can contain a nested directory, for example, and the check is in the spirit if '/' in request.path(I don’t remember exactly if Jinja2 can do such checks, but if that you can write a function).
But there will be another problem, that there are paths that partially coincide, then the check will be done in both cases, so IMHO, it's better to raise it from a template that inherits or directly from a function that starts rendering an identifier, the check of which will be in the template in this navigation.
Let's say in routes:

...
render_template('template', current='home')

And in the templates themselves:
<nav class="navigation block">
    <a class="nav-link {% if current == 'home' %}active{% endif %}" href="/">Главная</a>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question