# gulp-nunjucks-render

> Render Nunjucks templates with data

Latest version **2.2.3** (published 2019-03-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install gulp-nunjucks-render
pnpm add gulp-nunjucks-render
yarn add gulp-nunjucks-render
bun add gulp-nunjucks-render
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.2.3 |
| Published | 2019-03-18 |
| First published | 2014-02-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 5 |
| Unpacked size | 8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 150 |
| Maintainers | carlosl, kristijanhusak |
| Keywords | gulpplugin, nunjucks, template, templating, view, render, html, javascript |

## Links

- npm: https://www.npmjs.com/package/gulp-nunjucks-render
- Repository: https://github.com/carlosl/gulp-nunjucks-render
- Issues: https://github.com/carlosl/gulp-nunjucks-render/issues
- npm.io page: https://npm.io/package/gulp-nunjucks-render

## Dependencies (5)

- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [nunjucks](https://npm.io/package/nunjucks.md) ^3.1.2
- [through2](https://npm.io/package/through2.md) ^2.0.3
- [replace-ext](https://npm.io/package/replace-ext.md) ^1.0.0
- [plugin-error](https://npm.io/package/plugin-error.md) ^1.0.1

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 2.2.3 (latest) — 2019-03-18
- 2.2.2 — 2018-01-05
- 2.2.1 — 2017-04-07
- 2.2.0 — 2017-04-07
- 2.1.0 — 2017-04-07
- 2.0.0 — 2016-02-09
- 1.1.10 — 2016-02-08
- 1.1.0 — 2016-01-11
- 1.0.0 — 2015-09-14
- 0.2.1 — 2015-03-07
- 0.2.0 — 2015-03-07
- 0.1.3 — 2014-10-09
- 0.1.2 — 2014-09-02
- 0.1.1 — 2014-08-22
- 0.1.0 — 2014-07-07
- … 3 more at https://npm.io/package/gulp-nunjucks-render/versions

## README

[![Build Status](https://travis-ci.org/carlosl/gulp-nunjucks-render.svg?branch=master)](https://travis-ci.org/carlosl/gulp-nunjucks-render)

# [gulp](https://github.com/wearefractal/gulp)-nunjucks-render

> Render [Nunjucks](http://mozilla.github.io/nunjucks/) templates

*Issues with the output should be reported on the Nunjucks [issue tracker](https://github.com/mozilla/nunjucks/issues).*


## Install

Install with [npm](https://www.npmjs.com/package/gulp-nunjucks-render)

```
npm install --save-dev gulp-nunjucks-render
```


## Example

```js
var gulp = require('gulp');
var nunjucksRender = require('gulp-nunjucks-render');

gulp.task('default', function () {
  return gulp.src('src/templates/*.html')
    .pipe(nunjucksRender({
      path: ['src/templates/'] // String or Array
    }))
    .pipe(gulp.dest('dist'));
});
```

## Example with gulp data

```js
var gulp = require('gulp');
var nunjucksRender = require('gulp-nunjucks-render');
var data = require('gulp-data');

function getDataForFile(file) {
  return {
    example: 'data loaded for ' + file.relative
  };
}

gulp.task('default', function () {
	return gulp.src('src/templates/*.html')
    .pipe(data(getDataForFile))
    .pipe(nunjucksRender({
      path: 'src/templates'
    }))
    .pipe(gulp.dest('dist'));
});
```


## API

## Options
Plugin accepts options object, which contain these by default:

```js
var defaults = {
  path: '.',
  ext: '.html',
  data: {},
  inheritExtension: false,
  envOptions: {
    watch: false
  },
  manageEnv: null,
  loaders: null
};
```

* `path` - Relative path to templates
* `ext` - Extension for compiled templates, pass null or empty string if yo don't want any extension
* `data` - Data passed to template
* `inheritExtension` - If true, uses same extension that is used for template
* `envOptions` - These are options provided for nunjucks Environment. More info [here](https://mozilla.github.io/nunjucks/api.html#configure).
* `manageEnv` - Hook for managing environment before compilation. Useful for adding custom filters, globals, etc. Example [below](#environment)
* `loaders` - If provided, uses that as first parameter to Environment constructor. Otherwise, uses provided `path`. More info [here](https://mozilla.github.io/nunjucks/api.html#environment)

For more info about nunjucks functionality, check [https://mozilla.github.io/nunjucks/api.html](https://mozilla.github.io/nunjucks/api.html) and also a source code of this plugin.


### Data
U can pass data as option, or you can use gulp-data like in example above.

```js
nunjucksRender({data: {
  css_path: 'http://company.com/css/'
}});
```

For the following template
```html
<link rel="stylesheet" href="{{ css_path }}test.css" />
```

Would render
```html
<link rel="stylesheet" href="http://company.com/css/test.css" />
```

### Environment

If you want to manage environment (add custom filters or globals), you can to that with `manageEnv` function hook:

```javascript
var manageEnvironment = function(environment) {
  environment.addFilter('slug', function(str) {
    return str && str.replace(/\s/g, '-', str).toLowerCase();
  });

  environment.addGlobal('globalTitle', 'My global title')
}

nunjucksRender({
  manageEnv: manageEnvironment
}):
```

After adding that, you can use them in template like this:

```html
<h1>{{ globalTitle }}</h1>
<h3>{{ 'My important post'|slug }}</h3>
```

And get this result:

```html
<h1>My global title</h1>
<h3>my-important-post</h3>
```

## License

MIT © [Carlos G. Limardo](http://limardo.org) and [Kristijan Husak](http://kristijanhusak.com)

## Shout-outs

[Sindre Sorhus](http://sindresorhus.com/) who wrote the original [gulp-nunjucks](https://www.npmjs.org/package/gulp-nunjucks) for precompiling Nunjucks templates. I updated his to render instead of precompile.

[kristijanhusak](http://github.com/kristijanhusak) for bug fixes and help with maintenance.

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