queckezz/koa-views

How to use layout with handlebars?

timothykibler opened this issue ยท 3 comments

Used koa-generator (koa2 -e --hbs) to generate base code, it generates this:

app.use(views(__dirname + '/views', {
  extension: 'hbs',
  map: { hbs: 'handlebars' }
}));

By default this doesn't render the layout.hbs at all so I changed it to this:

app.use(views(__dirname + '/views', {
  extension: 'hbs',
  map: { hbs: 'handlebars' },
  layout: 'layout'
}))

Adding layoutPath: __dirname + 'views/' doesn't seem to work either.

Any ideas or am I doing something wrong here?

layout and layoutPath is no option in handlebars. What are you trying to do? The following renders the file layout.hbs as expected:

app.use(views(__dirname + '/views', {
  extension: 'hbs',
  map: { hbs: 'handlebars' }
}))

app.use(async function (ctx, next) {
  await ctx.render('./layout')
})

I was trying to use the layout.hbs the same way that pug uses a layout.pug, which I realize now is handled differently in hbs. It looks like I should be using partials instead and I can accomplish what I want from your response in #74. Thanks.