1 | # gulp-nunjucks-api
|
2 |
|
3 | > Render [Nunjucks](https://mozilla.github.io/nunjucks/) templates with data,
|
4 | custom filters, custom context functions and options for other Nunjucks API
|
5 | features.
|
6 |
|
7 | ## Install
|
8 |
|
9 | Install with [npm](https://npmjs.org/package/gulp-nunjucks-api)
|
10 |
|
11 | ```
|
12 | npm install --save-dev gulp-nunjucks-api
|
13 | ```
|
14 |
|
15 | ## Example
|
16 |
|
17 | ```js
|
18 | var gulp = require('gulp');
|
19 | var nunjucksRender = require('gulp-nunjucks-api');
|
20 |
|
21 | gulp.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
|
36 | var gulp = require('gulp');
|
37 | var nunjucksRender = require('gulp-nunjucks-api');
|
38 | var data = require('gulp-data');
|
39 |
|
40 | function getDataForFile(file){
|
41 | return {
|
42 | example: 'data loaded for ' + file.relative
|
43 | };
|
44 | }
|
45 |
|
46 | gulp.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 |
|
61 | Renders source templates using the given options to configure the Nunjucks API
|
62 | with custom data, extensions, filters and contextual functions.
|
63 |
|
64 | Same 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
|
70 | characters 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
|
73 | tags. See
|
74 | [Customizing Syntax](http://mozilla.github.io/nunjucks/api.html#customizing-syntax).
|
75 |
|
76 | With 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
|
82 | context.
|
83 | - **errors** _(default: true)_ Boolean. Whether to emit errors to gulp or not.
|
84 | Set to `false` to let the gulp task continue on errors. See also: the verbose
|
85 | option.
|
86 | - **extensions** _(default: {})_ Object. Global extensions added to the
|
87 | Nunjucks environment. See
|
88 | [Custom Tags](http://mozilla.github.io/nunjucks/api.html#custom-tags).
|
89 | - **filters** _(default: {})_ Object. Global filter functions added to the
|
90 | Nunjucks environment. See
|
91 | [Custom Filters](http://mozilla.github.io/nunjucks/api.html#custom-filters).
|
92 | - **functions** _(default: {})_ Object. Global functions merged into the
|
93 | Nunjucks render context.
|
94 | - **globals** _(default: undefined)_ Object. A single object which provides
|
95 | `data`, `extensions`, `filters` and `functions` objects instead of setting
|
96 | each of these options separately. The separate global options are merged into
|
97 | this base object.
|
98 | - **locals** _(default: undefined)_ Boolean or String. When `true`, enables
|
99 | loading of local template context data and functions from files that match
|
100 | the following default pattern: `"<filename>.+(js|json)"`. When a
|
101 | [glob pattern](https://github.com/isaacs/node-glob#glob-primer)
|
102 | string is given, the directory containing a given template will be searched
|
103 | using the pattern. Data and functions from all matched files are merged into
|
104 | the render context. Note that the token `<filename>` will be replaced with a
|
105 | given template's file name including extension. Use the `<filename_noext>`
|
106 | token instead in a custom pattern to target the file name without extension.
|
107 | - **verbose** _(default: false)_ Boolean. When `true`, detailed operational
|
108 | data is logged to the console.
|
109 |
|
110 | ### Render with data example
|
111 | ```
|
112 | nunjucksRender({
|
113 | data: {css_path: 'http://company.com/css/'}
|
114 | });
|
115 | ```
|
116 |
|
117 | For the following template
|
118 | ```
|
119 | <link rel="stylesheet" href="{{ css_path }}test.css" />
|
120 | ```
|
121 |
|
122 | Would render
|
123 | ```
|
124 | <link rel="stylesheet" href="http://company.com/css/test.css" />
|
125 | ```
|
126 |
|
127 | ### Watch mode
|
128 | Nunjucks' watch feature, which is normally enabled by default, is disabled by
|
129 | default for gulp. Pass `watch: true` to enable it:
|
130 |
|
131 | ```
|
132 | nunjucksRender({
|
133 | src: './source',
|
134 | watch: true
|
135 | });
|
136 | ```
|
137 |
|
138 | ## License
|
139 |
|
140 | MIT © [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)
|
146 | which 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
|
150 | Nunjucks templates.
|