Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a1a9250

Browse files
authored
check for active children in submenus (alshedivat#1869)
This patch checks, if the title of the current page matches with the title of a submenu child. If so, it sets the submenu and the corresponding child as active, highlighting it in the header.
1 parent 1fa3feb commit a1a9250

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

_includes/header.html

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@
6767
{%- for p in sorted_pages -%}
6868
{%- if p.nav and p.autogen == nil -%}
6969
{%- if p.dropdown %}
70-
<li class="nav-item dropdown {% if page.title == p.title %}active{% endif %}">
70+
{%- assign has_active_child = false -%}
71+
{%- for child in p.children -%}
72+
{%- if page.title == child.title -%}
73+
{%- assign has_active_child = true -%}
74+
{%- endif -%}
75+
{%- endfor -%}
76+
<li class="nav-item dropdown {% if page.title == p.title or has_active_child %}active{% endif %}">
7177
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ p.title }}
72-
{%- if page.title == p.title -%}
78+
{%- if page.title == p.title or has_active_child -%}
7379
<span class="sr-only">(current)</span>
7480
{%- endif -%}
7581
</a>
@@ -78,7 +84,7 @@
7884
{%- if child.title == 'divider' %}
7985
<div class="dropdown-divider"></div>
8086
{%- else %}
81-
<a class="dropdown-item" href="{{ child.permalink | relative_url }}">{{ child.title }}</a>
87+
<a class="dropdown-item {% if page.title == child.title %}active{% endif %}" href="{{ child.permalink | relative_url }}">{{ child.title }}</a>
8288
{%- endif -%}
8389
{% endfor %}
8490
</div>

0 commit comments

Comments
 (0)