https://github.com/d-band/koa-view
Template rendering middleware for koa (using nunjucks).
https://github.com/d-band/koa-view
koa koa-view koa2 nunjucks
Last synced: 8 days ago
JSON representation
Template rendering middleware for koa (using nunjucks).
- Host: GitHub
- URL: https://github.com/d-band/koa-view
- Owner: d-band
- Created: 2016-01-29T04:42:31.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2020-07-16T09:52:56.000Z (almost 5 years ago)
- Last Synced: 2024-05-01T22:51:48.184Z (12 months ago)
- Topics: koa, koa-view, koa2, nunjucks
- Language: JavaScript
- Homepage:
- Size: 67.4 KB
- Stars: 13
- Watchers: 6
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-koa - koa-view - [Nunjucks](http://mozilla.github.io/nunjucks/)视图渲染中间件。   (仓库 / 中间件)
README
# koa-view
[](https://www.npmjs.com/package/koa-view)
[](https://david-dm.org/d-band/koa-view)
[](https://travis-ci.org/d-band/koa-view)
[](https://coveralls.io/github/d-band/koa-view?branch=master)
[](https://greenkeeper.io/)> Template rendering middleware for koa (using [nunjucks](https://github.com/mozilla/nunjucks)).
## Installation
```
$ npm install koa-view
```## [Example](./examples/simple)
```js
const view = require('koa-view');// Must be used before any router is used
app.use(view(__dirname + '/views'));app.use(async function (ctx) {
ctx.state = {
session: ctx.session,
title: 'app'
};await ctx.render('user', {
user: 'Coder'
});
});
```More examples: [tests](./test/index.js)
## API
#### `view(root, opts)`
* `root`: (default `views`) Views location. All view you `render()` are relative to this path.
* `opts` [nunjucks configure opts](http://mozilla.github.io/nunjucks/api.html#configure)
* `opts.ext`: (default `html`) Extension for your view```js
// instead of this
await ctx.render('user.html')
// you can
await ctx.render('user')
```## Koa 1 Support
To use `koa-view` with koa@1, please use [koa-view 1.x](https://github.com/d-band/koa-view/tree/v1.x).
```bash
npm install koa-view@1 --save
```## License
MIT