Skip to content

Instantly share code, notes, and snippets.

@andylshort
Created January 29, 2025 18:22
Show Gist options
  • Save andylshort/8e0960e0ddcc332db5cf1c3a1d3da07c to your computer and use it in GitHub Desktop.
Save andylshort/8e0960e0ddcc332db5cf1c3a1d3da07c to your computer and use it in GitHub Desktop.
Nested navigation bar using Python and Jinja based on a JSON object
{% set active_page = active_page|default('') -%}
<nav role="navigation">
<ul>
{%- for nav_link in my_nav recursive -%}
<li><a class="nav-link{% if nav_link.id == active_page %} nav-active{% endif %}" href="{{ nav_link.url|e }}">{{ nav_link.title|e }}</a>
{%- if nav_link.children -%}
<ul>{{ loop(nav_link.children) }}</ul>
{%- endif %}
</li>
{%- endfor %}
</ul>
</nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment