Multiple layouts in hapijs

351 views Asked by At

I am trying to have multiple layout in a hapi application. I have 2 layout files: account, default

here is my view engine registration:

server.views({
    engines: { html: require('handlebars') },
    relativeTo: __dirname,
    path: './views',
    layoutPath: './views/layout',
    layout: 'default'
    //helpersPath: 'views/helpers',
    //partialsPath: 'views/partials'
});

by default it grabs default, how do I force it for a specific file to show another layout?

I also tried adding it to the view call and it didnt work:

module.exports.index = function (request, reply) {
    reply.view("home/index", {layout: 'account'});
}
1

There are 1 answers

0
Matt Harrison On BEST ANSWER

The second argument to reply.view() is the context object. So what you're doing in your example is providing a context with a layout property, that's why it doesn't work. The options should be the third argument:

reply.view(template, [context, [options]])

If you have no context, you can provide an empty object. This should work:

module.exports.index = function (request, reply) {

    reply.view('home/index', {}, { layout: 'account' });
};