让我们说这是代码片段:
<ul>
{% assign navigation = site.data.nav %}
{% for link in navigation %}
{% assign active = nil %}
{% for sublink in link.submenu %}
{% if page.url contains sublink.url %}
{% assign active = 'active' %}
{% endif %}
{% endfor %}
{% if page.url contains link.url %}
{% assign active = 'active' %}
{% endif %}
<li class="{{ active }}>
<a href="{{ site.baseurl }}{{ link.url }}">{{ link.text }}</a>
{% if link.submenu %}
<ul>
{% for sublink in link.submenu %}
<li {% if page.url contains sublink.url %}class="active"{% endif %}>
<a href="{{ site.baseurl }}{{ sublink.url }}">{{ sublink.text }}</a>
</li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
</ul>
nav.yml文件如下所示:
- text: First Menu
url: /modules/some text/introduction/
- text: Second Menu
url: /modules/some more text/intro-text/
submenu:
- text: Level Two First
url: /modules/level two first/intro-text/
- text: Level Two Second
url: /modules/level two second/intro-text/
因此,每个菜单项都会渲染,并且工作正常。但我在这里遇到的唯一问题是变量active是不会根据那些而不是保持零更新。即使page.url包含sublink.url或link.url,以下行也没有获得活动类的呈现
<li {% if page.url contains sublink.url %}class="active"{% endif %}>
可能是什么问题?