I’ll be honest, I was trying to help you along the path you had chosen, but the way I usually do this is not via the page path (because paths change), but via a variable in the front-matter which I can fix and can assume will not change on a whim. Below is some code I’ve from one of my sites that illustrates how you might do what you’re trying to do using my standard go-to:
In the helpers section of config (or you can put this in custom_helpers.rb file):
helpers do
def nav_active(slug, classes = [])
page_is?(slug) ? classes << "active" : classes
end
def page_is?(slug)
current_page.data.slug.include?(slug)
end
end
The first helper method is written with the “classes” default parameter the way it is in order to allow you to pass in other dynamic classes, as necessary, and preserve them with what gets added if the link matches the active page.
The second is written the way it is in order to capture potentially multiple slugs as “active”.
I have split them out into two methods for sake of clarity of my code.
In the navigation HTML:
[li]
[a href="page1", class=[%= nav_active("page1") %]] Page One[/a]
[ul]
[li]
[a href="page1a", class=[%= nav_active("page1a") %]] Page One - A[/a]
[/li]
[li]
[a href="page1b", class=[%= nav_active("page1b") %]] Page One - B[/a]
[/li]
[/li]
[li]
[a href="page2", class=[%= nav_active("page2") %]] Page Two[/a]
[/li]
Here I have replaced “<” and “>” with square brackets so it doesn’t show up as actual page HTML.
In the front matter for page1.html
—
slug: “page1”
—
In the front matter for page1a.html
—
slug: “page1 page1a”
—
In the front matter for page1b.html
—
slug: “page1 page1b”
—
In the front matter for page2.html
—
slug: “page2”
—
So, as you can see, the helper method will add “active” to the nav classes for both the PAGE ONE link and the PAGE ONE A link when the current active page is page1a.html.