Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
177
rated 0 times [  179] [ 2]  / answers: 1 / hits: 15551  / 10 Years ago, fri, november 21, 2014, 12:00:00

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:



// About
exports.about = function(req, res) {
res.render('content/about.ejs', {
title: 'About'
});
};


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



<ul class=nav id=nav>
<li><a href=/>Home</a></li>
<li class=current><a href=/about>About</a></li>
<li><a href=/>Contact</a></li>
</ul>


Any ideas of how to achieve this?


More From » node.js

 Answers
13

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



<li <% if (page_name === 'about') { %>class=current <% } %> ><a href=/about>About</a></li>


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


[#68747] Tuesday, November 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deanna

Total Points: 84
Total Questions: 86
Total Answers: 107

Location: Cyprus
Member since Wed, Dec 8, 2021
3 Years ago
;