Multiple layouts with Handlebars and ExpressJS? Multiple layouts with Handlebars and ExpressJS? express express

Multiple layouts with Handlebars and ExpressJS?


You should be able to pass in the layout from your route/controller when you call the render method.

router.get('/', function(req, res) {    res.render('home', {layout: 'viewBLayout.hbs'});});

I am pretty sure jade will let you switch layouts from inside the template but I don't know if you can do that with handlebars.


If you use express-hbs, you can specify a layout in the template with a comment like:

{{!< layout}}

Alternatively, you can try exphbs. It also supports layout comments and multiple layouts can be nested. (Disclaimer: I wrote it.)


make sure you first make two files named "main.handlebars" and "backend.handlebars" in /layouts directory:

Try this code for two routes if you want for example

router.get('/', function(req, res) {    res.render('home', {layout: 'main'});});router.get('/backend', function(req, res) {    res.render('home', {layout: 'backend'});});