I am trying to add an “active” class to the categories on my main menu when the category is opened and also for the category when a single post in that category is opened. All posts in the _posts
folder are in folders bearing the category names. I can get the active class to work when I hard code all the menu items like this
<ul class="nav-menu">
<li {% if page.url contains '/category/name/' or page.path contains "_posts/category-name" %}class="active" aria-current="page"{% endif %}><a href="/category/name/">Category Name</a></li>
</ul>
But it does not work if I create the navigation inside a nav.yaml file like this
- title: Category Name
url: /category/name/
path: _posts/category-name
and then add this to header.html
<ul class="nav-menu">
{% for nav in site.data.nav %}
<li {% if page.url contains '{{nav.url}}' or page.path contains "{{nav.path}}" %}class="active" aria-current="page"{% endif %}><a href="{{nav.url}}">{{nav.title}}</a></li>
{% endfor %}
</ul>
I am trying not to hard code the menu items, so my question is, how do I get the items in the _data
file to work as expected?