UNPKG

4.86 kBMarkdownView Raw
1# gulp-nunjucks-api
2
3> Render [Nunjucks](https://mozilla.github.io/nunjucks/) templates with data,
4custom filters, custom context functions and options for other Nunjucks API
5features.
6
7## Install
8
9Install with [npm](https://npmjs.org/package/gulp-nunjucks-api)
10
11```
12npm install --save-dev gulp-nunjucks-api
13```
14
15## Example
16
17```js
18var gulp = require('gulp');
19var nunjucksRender = require('gulp-nunjucks-api');
20
21gulp.task('default', function () {
22 return gulp.src('src/templates/*.html')
23 .pipe(nunjucksRender({
24 src: 'src/templates',
25 data: require('./global-data.json'),
26 filters: require('./global-filters.js'),
27 functions: require('./global-functions.js')
28 }))
29 .pipe(gulp.dest('dist'));
30});
31```
32
33## Example with gulp data
34
35```js
36var gulp = require('gulp');
37var nunjucksRender = require('gulp-nunjucks-api');
38var data = require('gulp-data');
39
40function getDataForFile(file){
41 return {
42 example: 'data loaded for ' + file.relative
43 };
44}
45
46gulp.task('default', function () {
47 return gulp.src('src/templates/*.html')
48 .pipe(data(getDataForFile))
49 .pipe(nunjucksRender({
50 src: 'src/templates/'
51 }))
52 .pipe(gulp.dest('dist'));
53});
54```
55
56
57## API
58
59### gulp-nunjucks-api(options)
60
61Renders source templates using the given options to configure the Nunjucks API
62with custom data, extensions, filters and contextual functions.
63
64Same options as
65[`nunjucks.configure()`](http://mozilla.github.io/nunjucks/api.html#configure):
66
67- **watch** _(default: false)_ reload templates when they are changed.
68- **express** an express app that nunjucks should install to.
69- **autoescape** _(default: false)_ controls if output with dangerous
70characters are escaped automatically. See
71[Autoescaping](http://mozilla.github.io/nunjucks/api.html#autoescaping).
72- **tags** _(default: see nunjucks syntax)_ defines the syntax for nunjucks
73tags. See
74[Customizing Syntax](http://mozilla.github.io/nunjucks/api.html#customizing-syntax).
75
76With the following additional options:
77
78- **extension** _(default: ".html")_ String. File extension to output.
79- **src** _(default: undefined)_ String or Array. Search path(s) for
80`nunjucks.configure()`.
81- **data** _(default: {})_ Ojbect. Global data merged into the Nunjucks render
82context.
83- **errors** _(default: true)_ Boolean. Whether to emit errors to gulp or not.
84Set to `false` to let the gulp task continue on errors. See also: the verbose
85option.
86- **extensions** _(default: {})_ Object. Global extensions added to the
87Nunjucks environment. See
88[Custom Tags](http://mozilla.github.io/nunjucks/api.html#custom-tags).
89- **filters** _(default: {})_ Object. Global filter functions added to the
90Nunjucks environment. See
91[Custom Filters](http://mozilla.github.io/nunjucks/api.html#custom-filters).
92- **functions** _(default: {})_ Object. Global functions merged into the
93Nunjucks render context.
94- **globals** _(default: undefined)_ Object. A single object which provides
95`data`, `extensions`, `filters` and `functions` objects instead of setting
96each of these options separately. The separate global options are merged into
97this base object.
98- **locals** _(default: undefined)_ Boolean or String. When `true`, enables
99loading of local template context data and functions from files that match
100the following default pattern: `"<filename>.+(js|json)"`. When a
101[glob pattern](https://github.com/isaacs/node-glob#glob-primer)
102string is given, the directory containing a given template will be searched
103using the pattern. Data and functions from all matched files are merged into
104the render context. Note that the token `<filename>` will be replaced with a
105given template's file name including extension. Use the `<filename_noext>`
106token instead in a custom pattern to target the file name without extension.
107- **verbose** _(default: false)_ Boolean. When `true`, detailed operational
108data is logged to the console.
109
110### Render with data example
111```
112nunjucksRender({
113 data: {css_path: 'http://company.com/css/'}
114});
115```
116
117For the following template
118```
119<link rel="stylesheet" href="{{ css_path }}test.css" />
120```
121
122Would render
123```
124<link rel="stylesheet" href="http://company.com/css/test.css" />
125```
126
127### Watch mode
128Nunjucks' watch feature, which is normally enabled by default, is disabled by
129default for gulp. Pass `watch: true` to enable it:
130
131```
132nunjucksRender({
133 src: './source',
134 watch: true
135});
136```
137
138## License
139
140MIT © [Devoptix LLC](http://www.devoptix.com)
141
142## Shout-outs
143
144[Carlos G. Limardo](http://limardo.org) who wrote
145[gulp-nunjucks-render](https://www.npmjs.com/package/gulp-nunjucks-render)
146which I am forking in order to update Nunjucks and do other stuff.
147
148[Sindre Sorhus](http://sindresorhus.com/) who wrote the original
149[gulp-nunjucks](https://www.npmjs.org/package/gulp-nunjucks) for precompiling
150Nunjucks templates.