Set engine options
Closed this issue ยท 5 comments
Is there a way to set options to the template engine?
I'm using nunjucks and I want to set some options, like I would do with nunjucks.configure.
I would like this feature as well. I checked and it isn't currently supported (see https://github.com/queckezz/koa-views/blob/master/index.js#L133)
The second parameter of state
passed in to consolidate is a combination of ctx.state
and whatever local values you pass in to render
. You can create a simple middleware that adds whatever settings you need to ctx.state
, depending on what values consolidate is expecting. Check consolidate to see how it uses those values. For instance for nunjucks you can see it's expecting a settings
field so your middleware would look something like this:
app.use((ctx, next) => {
ctx.state.settings = {
views: your.parameters.here
};
return next();
}
Admittedly this is not perfect as you have to make sure that the field names on ctx.state
don't overwrite something elsewhere in your application but it works.
+1 this is required for any use of partials...
+1 @phairoh Thank you for the example. Any plans to implement this directly?
You can now add engine specific options via options
as follows:
const app = new Koa()
.use(views(__dirname, {
map: { hbs: 'handlebars' },
options: {
helpers: {
uppercase: (str) => str.toUpperCase()
},
partials: {
subTitle: './my-partial' // requires ./my-partial.hbs
}
}
}))
.use(function (ctx) {
ctx.state = { title: 'my title', author: 'queckezz' }
return ctx.render('./my-view.hbs')
})