Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 2 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 (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-07-16T09:52:56.000Z (over 4 years ago)
- Last Synced: 2024-05-01T22:51:48.184Z (9 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/)视图渲染中间件。 ![](https://img.shields.io/github/stars/d-band/koa-view.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koa-view.svg?style=flat-square) (仓库 / 中间件)
README
# koa-view
[![NPM version](https://img.shields.io/npm/v/koa-view.svg)](https://www.npmjs.com/package/koa-view)
[![Dependency Status](https://david-dm.org/d-band/koa-view.svg)](https://david-dm.org/d-band/koa-view)
[![Build Status](https://travis-ci.org/d-band/koa-view.svg?branch=master)](https://travis-ci.org/d-band/koa-view)
[![Coverage Status](https://coveralls.io/repos/github/d-band/koa-view/badge.svg?branch=master)](https://coveralls.io/github/d-band/koa-view?branch=master)
[![Greenkeeper badge](https://badges.greenkeeper.io/d-band/koa-view.svg)](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