NOTE: you must still install the engines you wish to use, add them to your package.json dependencies.
Example
varviews=require('koa-views');constrender=views(__dirname+'/views',{map: {html: 'underscore'}})// Must be used before any router is usedapp.use(render)// OR Expand by app.context// No order restrictions// app.context.render = render()app.use(asyncfunction(ctx){ctx.state={session: this.session,title: 'app'};awaitctx.render('user',{user: 'John'});});
For more examples you can take a look at the tests.
Simple middleware
If you need to simply render pages with locals, you can install koa-views-render:
npm install koa-views-render
Then simply use it on your routes and its arguments will be passed to ctx.render.
opts.engineSource: replace consolidate as default engine source
If you’re not happy with consolidate or want more control over the engines, you can override it with this options. engineSource should
be an object that maps an extension to a function that receives a path and options and returns a promise. In this example templates with the foo extension will always return bar.
请发表评论