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: 5 months 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 10 years ago)
- Default Branch: master
- Last Pushed: 2025-08-02T10:11:10.000Z (6 months ago)
- Last Synced: 2025-08-02T12:27:52.182Z (6 months ago)
- Topics: koa, koa-view, koa2, nunjucks
- Language: JavaScript
- Homepage:
- Size: 69.3 KB
- Stars: 12
- Watchers: 5
- Forks: 2
- Open Issues: 2
-
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