I am using github to host a static site and Jekyll to generate it.
I have a menu bar (as <ul>) and would like the <li> corresponding to the current page to be assigned a different class for CSS highlighting.
So something like pseudo code:
<li class={(hrefpage==currentpage)?"highlight":"nothighlight"} ...>
Or perhaps even generate the whole <ul> in Jekyll.
How can this be done with minimal changes outside of the offending <ul>?
Yes you can do this.
To accomplish this we will take advantage of the fact that the current page is always represented by the liquid variable:
pagein the template, and also that each post/page has a unique identifier in itspage.urlattribute.This means that we just have to use a loop to build our navigation page, and by doing so we can check
page.urlagainst every member of the loop. If it finds a match, it knows to highlight that element. Here we go:This works as expected. However you probably don’t want all your page’s in your nav bar. In order to emulate page “grouping” you can something like this:
Now pages can be “grouped”. To give a page a group you need to specify it in the pages YAML Front Matter:
Finally you can use your new code!
Wherever you need your navigation to go in your template, simply “call” your include file and pass it some pages and the group you want to display:
Examples
This functionality is part of the Jekyll-Bootstrap framework.
You can see exact documentation for the code outlined here: http://jekyllbootstrap.com/api/bootstrap-api.html#jbpages_list
Finally you can see it in action within the website itself. Just look at the righthand navigation and you will see the current page is highlighted in green: Example highlighted nav link