In my NodeJS-express app, I'm using handlebars engine for templating. Routing with dynamic data is possible. I have one single layout file for my application. How can I pass data to template file dynamically?
Edits:
Here's my Template file.
<!DOCTYPE html> <html> <head> </head> <body> <nav> <div> <ul class="navbar-nav"> <li class="nav-item active"> <a>Home</a> </li> <li class="nav-item"> <a>about</a> </li> <li class="nav-item"> <a>contact</a> </li> </ul> </div> </nav> {{{body}}} </body> </html>
I need the nav Items(home, about, contact) are all from API call. not hardcoded.
I think you need to do this in your handlebars:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<nav>
{{{navStuff}}}
</nav>
{{{body}}}
</body>
</html>
And in your Express route, you can do this: (I assume its simple routing. You can adapt for ReactRouter V4)
app.get('/home', function(req, res, next){
return res.render('app',{
body: toRender,
navStuff: menu
})
});
Similarly, if you have to make a databse call to fetch menu items based on page then you can adopt to this:
app.get('/home', function(req, res, next){
getMenuFromDb(pathName).then(function(menu){
return res.render('app',{
body: toRender,
navStuff: menu
})
});
})
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.