Skip to content
Advertisement

Node.js, Express, EJS – Active class on current page in navigation

I’d like to render a ‘current’ class on each of my navigation links depending on which page I’m on in my layout.ejs template.

Currently, my express controller index looks like this:

JavaScript

And in my layout.ejs I have the following, which I’d like be rendered dynamically.

JavaScript

Any ideas of how to achieve this?

Advertisement

Answer

You could include a page_name: 'about' in the res.render data and then in the template something like:

JavaScript

I didn’t test the syntax, but that’s the gist.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement