How to change default layout in Express using Handlebars?

Sometimes, we want to change default layout in Express using Handlebars.

In this article, we’ll look at how to change default layout in Express using Handlebars.

How to change default layout in Express using Handlebars?

To change default layout in Express using Handlebars, we can res.render with the layout option.

For instance, we write

res.render('view', {
  title: 'my other page',
  layout: 'other'
});

to call res.render with the layout set to other to render other.hbs as the layout file.

We can override the layout for the whole app with app.set:

app.set('view options', { layout: 'other' });

Conclusion

To change default layout in Express using Handlebars, we can res.render with the layout option.