Jinja - Is there any built-in variable to get current HTML page name?

Viewed 25979

i'm very new to Jinja and Flask

I want to set different background color in the navigation bar to indicate the current page.

Is there any built-in Jinja variable or method that returns current HTML pages? If possible, I want the code that doesn't need to communicate with the Python file.

So if i'm currently in index.html, it will return "index" or "index.html"

Here's my navigation code in my template:

<ul>
   {% for item in navigation %}
       <a href="{{url_for(item.route)}}">
       <li>
           {{item.text}}
       </li>
       </a>
   {% endfor %}
</ul>

I want to add if statement so the current page will get <li> that has class

{% if ??? %}
   <li class="current">
   ...
   </li>
{% else %}
   ...
{% endif %}

Thank You

3 Answers

In Flask 2.0.1, the request object is available in the template. With this you can easily use it to check the page using the request.path attribute.

An example of a check would be like this:

{% if request.path == "/" %}
  <h1>You are at the root</h1>
{% endif %}
Related