Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/DigitalIO/koajs-nunjucks
Koa Nunjucks rendering middleware
https://github.com/DigitalIO/koajs-nunjucks
Last synced: 25 days ago
JSON representation
Koa Nunjucks rendering middleware
- Host: GitHub
- URL: https://github.com/DigitalIO/koajs-nunjucks
- Owner: DigitalIO
- License: mit
- Archived: true
- Created: 2014-12-09T20:39:13.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2014-12-13T02:42:25.000Z (about 10 years ago)
- Last Synced: 2024-05-19T04:48:37.490Z (7 months ago)
- Language: JavaScript
- Size: 645 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-koa - koajs-nunjucks - [Nunjucks](http://mozilla.github.io/nunjucks/)视图渲染中间件。 ![](https://img.shields.io/github/stars/Faeson/koajs-nunjucks.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/koajs-nunjucks.svg?style=flat-square) (仓库 / 中间件)
README
Koa.js Nunjucks
===============
[![NPM Version][npm-image]][npm-url]
[![Node Version][node-image]][npm-url]
[![License][license-image]][npm-url]## Installation
```
$ npm install koajs-nunjucks
```## Middleware Setup
First, require koajs-nunjucks. It returns a function that will return a middleware generator.```js
var KoajsNunjucks = require('koajs-nunjucks');
var fMiddleware = KoajsNunjucks('', {});
```The function takes two arguments. They are passed in the same order to the [nunjucks.configure()](http://mozilla.github.io/nunjucks/api.html#configure) function.
Next, "use" the middleware function (before any other middleware that attempts to use it).```js
var app = require('koa')();
app.use( fMiddleware );
```## Usage
The render function is attached to the Koa context. It is availiable in other middleware functions directly as "this". The function renders the view and automatically sets it as the response body.```js
yield this.render('home.nj', { username: 'Bob' });
```
The render function takes two arguments as defined by [nunjucks.render](http://mozilla.github.io/nunjucks/api.html#render). It does NOT take a callback.In additon to the render function, the [enviorment](http://mozilla.github.io/nunjucks/api.html#environment) is also attached to the context.
Every request creates a fresh enviorment.
```js
this.nunjucks
```# License
MIT[npm-image]: https://img.shields.io/npm/v/koajs-nunjucks.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koajs-nunjucks?style=flat-square
[license-image]: https://img.shields.io/npm/l/koajs-nunjucks.svg?style=flat-square
[node-image]: https://img.shields.io/node/v/koajs-nunjucks.svg?style=flat-square