# koa-nunjucks-2

> Lightweight Koa middleware for Nunjucks

Latest version **3.0.2** (published 2017-06-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install koa-nunjucks-2
pnpm add koa-nunjucks-2
yarn add koa-nunjucks-2
bun add koa-nunjucks-2
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2017-06-07 |
| First published | 2015-07-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 47 |
| Maintainers | strawbrary |
| Keywords | koa, nunjucks, view |

## Links

- npm: https://www.npmjs.com/package/koa-nunjucks-2
- Repository: https://github.com/strawbrary/koa-nunjucks-2
- Issues: https://github.com/strawbrary/koa-nunjucks-2/issues
- npm.io page: https://npm.io/package/koa-nunjucks-2

## Dependencies (6)

- [bluebird](https://npm.io/package/bluebird.md) ^3.1.1
- [nunjucks](https://npm.io/package/nunjucks.md) ^3.0.0
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.0.1
- [babel-runtime](https://npm.io/package/babel-runtime.md) ^6.9.2
- [lodash.defaults](https://npm.io/package/lodash.defaults.md) ^4.0.0
- [lodash.difference](https://npm.io/package/lodash.difference.md) ^4.0.1

## Recent versions

- 3.0.2 (latest) — 2017-06-07
- 3.0.0-alpha.5 (next) — 2016-11-05
- 3.0.1 — 2017-05-31
- 3.0.0 — 2017-05-31
- 2.2.0 — 2016-09-15
- 2.1.6 — 2016-07-20
- 3.0.0-alpha.4 — 2016-06-25
- 3.0.0-alpha.3 — 2016-05-09
- 2.1.5 — 2016-05-06
- 3.0.0-alpha.2 — 2016-04-01
- 2.1.4 — 2016-01-19
- 3.0.0-alpha.1 — 2016-01-19
- 2.1.3 — 2016-01-13
- 2.1.2 — 2015-12-07
- 2.1.1 — 2015-12-07
- … 6 more at https://npm.io/package/koa-nunjucks-2/versions

## README

# koa-nunjucks-2
Lightweight [Koa](http://koajs.com/) middleware for [Nunjucks](https://mozilla.github.io/nunjucks/).

[![NPM version][npm-img]][npm-url]
[![Build status][travis-img]][travis-url]
[![License][license-img]][license-url]
[![Dependency status][david-img]][david-url]

## Installation
```
npm install --save koa-nunjucks-2
```
NOTE: v3 requires Koa 2 or later. If you're using Koa 1, use v2 of this package.

## Usage
### Example
```js
const Koa = require('koa');
const app = new Koa();
const koaNunjucks = require('koa-nunjucks-2');
const path = require('path');

app.use(koaNunjucks({
  ext: 'html',
  path: path.join(__dirname, 'views'),
  nunjucksConfig: {
    trimBlocks: true
  }
}));

app.use(async (ctx) => {
  await ctx.render('home', {double: 'rainbow'});
});
```

### Config Options
* **ext** *(default: 'njk')*: Extension that will be automatically appended to the file name in `ctx.render` calls. Set to a falsy value to disable.
* **path** *(default: current directory)*: Path to the templates. Also supports passing an array of paths.
* **writeResponse** *(default: true)*: If true, writes the rendered output to `response.body`.
* **functionName** *(default: 'render')*: The name of the function that will be called to render the template.
* **nunjucksConfig**: Object of [Nunjucks config options](https://mozilla.github.io/nunjucks/api.html#configure).
* **configureEnvironment**: A function to modify the Nunjucks environment. See the [Extending Nunjucks](#Extending Nunjucks) section below for usage.

### Global Template Variables
Use [ctx.state](https://github.com/koajs/koa/blob/master/docs/api/context.md#ctxstate) to make a variable available in all templates.

### Extending Nunjucks
Use the `configureEnvironment` config option to define a function which will receive a [Nunjucks Environment](https://mozilla.github.io/nunjucks/api.html#environment) as its argument. This allows you to define custom filters, extensions etc.

```js
app.use(koaNunjucks({
  ext: 'html',
  path: path.join(__dirname, 'views'),
  configureEnvironment: (env) => {
    env.addFilter('shorten', (str, count) => {
      return str.slice(0, count || 5);
    });
  }
}));

```

## License
  [MIT][license-url]


[npm-img]: https://img.shields.io/npm/v/koa-nunjucks-2.svg?style=flat-square
[npm-url]: https://npmjs.org/package/koa-nunjucks-2
[travis-img]: https://img.shields.io/travis/strawbrary/koa-nunjucks-2/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/strawbrary/koa-nunjucks-2
[license-img]: https://img.shields.io/badge/license-MIT-green.svg?style=flat-square
[license-url]: LICENSE
[david-img]: https://img.shields.io/david/strawbrary/koa-nunjucks-2.svg?style=flat-square
[david-url]: https://david-dm.org/strawbrary/koa-nunjucks-2

---
_Source: https://npm.io/package/koa-nunjucks-2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
