I have a layout Jade view that has a menu via unordered list, and I want to set the <li> to be <li class="active">...</li> when the current page is rendered in the browser.
I assume I will have to access the current request to determine when to set the attribute on the <li>
I can’t find any examples of how to do this so hoping someone can help
Thanks
Try this before your call res.render() in your route:
or
Then you would have to do a bunch of if/else statements in your view (as the above solution suggests).
I however, prefer to do this on client side instead, to keep my template files free from as much logic as possible:
In page template file (this is ejs, not sure how to echo in jade):
Then with jQuery you can grab the path from body and attach an active class:
Update: You can also just use
var path = window.location.pathnameinstead of saving it to an attribute onbody