高亮活动菜单项

你经常想要一个带有活动导航项的导航栏。这相当容易实现。因为在 block 外 的声明在子模板中是全局的,并且在布局模板求值前执行,在子模板中定义活动的 菜单项:

{% extends "layout.html" %}
{% set active_page = "index" %}

布局模板之后就可以访问 active_page 。此外,这意味着你可以为它定义默认 值:

{% set navigation_bar = [
    ("/", "index", "Index"),
    ("/downloads/", "downloads", "Downloads"),
    ("/about/", "about", "About")
] -%}
{% set active_page = active_page|default("index") -%}
...
<ul id="navigation">
{% for href, id, caption in navigation_bar %}
  <li{% if id == active_page %} class="active"{% endif
  %}><a href="{{ href|e }}">{{ caption|e }}</a>/li>
{% endfor %}
</ul>
...
文章导航