使用下面的数据(链接)下面的Twig逻辑正在检查“链接”级别的名称,以比较传递给宏的参数。这会将“活动”类应用于相应的列表项。不幸的是,我不知道如何也检查“子链接”,因为“子链接”值可能与传递给宏的参数匹配。如果它是== currentPage的“子链接”(而不是“链接”),我还需要能够检查“子链接”并应用“活动”。
{% set
links = {
'home': ['home', 'Home'],
'hotel-reviews': ['hotel-reviews', 'Hotel Reviews'],
'pages': {
name: 'Pages',
sublinks: {
'about': ['samples/about', 'About Us'],
'register': ['samples/register', 'Register'],
'error': ['error', 'Error Page'],
'404': ['404', '404 Page'],
},
},
'portfolio': {
name: 'Portfolio',
sublinks: {
'portfolio': ['portfolio/portfolio', 'Portfolio'],
'project': ['portfolio/project', 'Project'],
},
},
'blog': {
name: 'Blog',
sublinks: {
'blog': ['blog/blog', 'Blog'],
'post': ['blog/post', 'Blog Post'],
},
},
'ui-elements': ['ui-elements', 'UI Elements'],
}
%}
{% set currentPage = this.page.id %}
{% macro render_menu(links, currentPage) %}
{% import _self as subnav %}
{% for code, link in links %}
<li class="{{ (code == currentPage) ? 'active' }} {{ link.sublinks ?
'dropdown' }}">
<a
href="{{ link.sublinks ? '#' : (link.page ?: link[0])|page }}"
{% if link.sublinks %}data-toggle="dropdown"{% endif %}
class="{{ link.sublinks ? 'dropdown-toggle' }}"
>
{{ link.name ?: link[1] }}
{% if link.sublinks %}<span class="caret"></span>{% endif %}
</a>
{% if link.sublinks %}
<span class="dropdown-arrow"></span>
<ul class="dropdown-menu">
{{ subnav.render_menu(link.sublinks) }}
</ul>
{% endif %}
</li>
{% endfor %}
{% endmacro %}
{% import _self as nav %}
<nav id="layout-nav" class="navbar navbar-inverse navbar-fixed-top"
role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse"
data-target=".navbar-main-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="fa fa-bars"></span>
</button>
<a class="navbar-brand" href="{{ 'home'|page }}">Taiga Travel</a>
</div>
<div class="collapse navbar-collapse navbar-main-collapse">
<ul class="nav navbar-nav navbar-right">
{{ nav.render_menu(links, currentPage) }}
<li>
<button
class="btn btn-sm navbar-btn btn-primary navbar-right
hidden-sm hidden-xs">
Sign in
</button>
</li>
</ul>
</div>
</div>
答案 0 :(得分:0)
经过一番摸索之后,我发现我会尝试遍历子链接以检查“ currentPage”,并且似乎已经奏效了:
{% set
links = {
'home': ['home', 'Home'],
'hotel-reviews': ['hotel-reviews', 'Hotel Reviews'],
'pages': {
name: 'Pages',
sublinks: {
'about': ['samples/about', 'About Us'],
'register': ['samples/register', 'Register'],
'error': ['error', 'Error Page'],
'404': ['404', '404 Page'],
},
},
'portfolio': {
name: 'Portfolio',
sublinks: {
'portfolio': ['portfolio/portfolio', 'Portfolio'],
'project': ['portfolio/project', 'Project'],
},
},
'blog': {
name: 'Blog',
sublinks: {
'blog': ['blog/blog', 'Blog'],
'post': ['blog/post', 'Blog Post'],
},
},
'ui-elements': ['ui-elements', 'UI Elements'],
}
%}
{% set currentPage = this.page.id %}
{% macro render_menu(links, currentPage) %}
{% import _self as subnav %}
{% for code, link in links %}
<li class="
{% if link.sublinks %}
{% for sub, sublink in link.sublinks %}
{{ (sub == currentPage) ? 'active' }} {{ link.sublinks ? 'dropdown' }}
{% endfor %}
{% else %}
{{ (code == currentPage) ? 'active' }} {{ link.sublinks ? 'dropdown' }}
{% endif %}"
>
<a
href="{{ link.sublinks ? '#' : (link.page ?: link[0])|page }}"
{% if link.sublinks %}data-toggle="dropdown"{% endif %}
class="{{ link.sublinks ? 'dropdown-toggle' }}"
>
{{ link.name ?: link[1] }}
{% if link.sublinks %}<span class="caret"></span>{% endif %}
</a>
{% if link.sublinks %}
<span class="dropdown-arrow"></span>
<ul class="dropdown-menu">
{{ subnav.render_menu(link.sublinks, currentPage) }}
</ul>
{% endif %}
</li>
{% endfor %}
{% endmacro %}
{% import _self as nav %}
<nav id="layout-nav" class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-main-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="fa fa-bars"></span>
</button>
<a class="navbar-brand" href="{{ 'home'|page }}">Taiga Travel</a>
</div>
<div class="collapse navbar-collapse navbar-main-collapse">
<ul class="nav navbar-nav navbar-right">
{{ nav.render_menu(links, currentPage) }}
<li>
<button
class="btn btn-sm navbar-btn btn-primary navbar-right hidden-sm hidden-xs">
Sign in
</button>
</li>
</ul>
</div>
</div>
</nav>