I have come up with the following code but the problem is, there will be duplication of the anchor tag for each menu item.Is there a better way to do this?
ul.nav
- if(menu="Home")
li.active
a(href="#") Dashboard
else
li
a(href="#") Dashboard
li
a(href="#") About
li
a(href="#") Contact
js server. The express. Router() function is used to create a new router object. This function is used when you want to create a new router object in your program to handle requests. Multiple requests can be easily differentiated with the help of the Router() function in Express.
Routing refers to how an application's endpoints (URIs) respond to client requests. For an introduction to routing, see Basic routing. You define routing using methods of the Express app object that correspond to HTTP methods; for example, app.get() to handle GET requests and app.post to handle POST requests.
It is possible to write a class in Node. js using JavaScript. JavaScript has OOP support when we use prototypes. Let's look at an example to understand how to write a class in Node.
Found this in another question that was similar:
Use a ternary at each "li"
ul
li(class=(title === 'Home' ? 'active' : ''))
a(href='#') Home
li(class=(title === 'Dashboard' ? 'active' : ''))
a(href='#') Dashboard
You can setup your routes to pass the "menu" value instead of using "title" if you want:
exports.index = function(req, res) {
res.render('index', {title: 'Home', menu: 'Home'});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With