UNPKG

3.65 kBMarkdownView Raw
1# Grunt task for rendering nunjucks` templates to HTML
2
3[![NPM version](https://badge.fury.io/js/grunt-nunjucks-2-html.png)](http://badge.fury.io/js/grunt-nunjucks-2-html)
4
5## Getting start
6
7If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide.
8
9Once plugin has been installed include it in your `Gruntfile.js`
10
11```javascript
12grunt.loadNpmTasks('grunt-nunjucks-2-html');
13```
14
15## Usage examples
16
17Task targets and options may be specified according to the grunt [Configuring tasks](http://gruntjs.com/configuring-tasks) guide.
18
19```javascript
20nunjucks: {
21 options: {
22 data: grunt.file.readJSON('data.json'),
23 paths: 'templates'
24 },
25 render: {
26 files: {
27 'index.html' : ['index.html']
28 }
29 }
30}
31```
32
33`templates/index.html` (relative to the gruntfile) is now compiled with `data.json`!
34
35```javascipt
36nunjucks: {
37 options: {
38 data: grunt.file.readJSON('data.json')
39 },
40 render: {
41 files: [
42 {
43 expand: true,
44 cwd: "bundles/",
45 src: "*.html",
46 dest: "build/",
47 ext: ".html"
48 }
49 ]
50 }
51```
52
53You'll get a set of html files in `build` folder.
54
55## Tests
56
57```bash
58$ npm test
59```
60
61## Options
62
63### Data
64
65Read JSON from file using `grunt.file.readJSON` or specify object just inside your `Gruntfile`.
66
67### preprocessData
68
69You should specify a function to construct each data object for every of your templates. Execution context for the function would be a [grunt file object](http://gruntjs.com/api/inside-tasks#this.files). If you specify a data option it would be passed inside the function as an argument.
70
71For instance, you could include name of the file inside an every data object
72
73```js
74nunjucks: {
75 options: {
76 preprocessData: function(data) {
77 var page = require('path').basename(this.src[0], '.html');
78 var result = {
79 page: page,
80 data: data
81 };
82 return result;
83 },
84 data: grunt.file.readJSON('data.json')
85 },
86 render: {
87 files: [
88 {
89 expand: true,
90 cwd: "bundles/",
91 src: "*.html",
92 dest: "build/",
93 ext: ".html"
94 }
95 ]
96 }
97}
98```
99
100### Paths
101
102You could specify root path for your templates, `paths` would be set for [nunjucks' configure](http://mozilla.github.io/nunjucks/api#configure)
103
104### Customizing Syntax
105
106If you want different tokens than {{ and the rest for variables, blocks, and comments, you can specify different tokens as the tags option:
107
108```js
109nunjucks: {
110 options: {
111 tags: {
112 blockStart: '<%',
113 blockEnd: '%>',
114 variableStart: '<$',
115 variableEnd: '$>',
116 commentStart: '<#',
117 commentEnd: '#>'
118 },
119 data: grunt.file.readJSON('data.json')
120 },
121 render: {
122 files: [
123 {
124 expand: true,
125 cwd: "bundles/",
126 src: "*.html",
127 dest: "build/",
128 ext: ".html"
129 }
130 ]
131 }
132}
133```
134
135### configureEnvironment
136
137You could use nunjucks' environment API to set some global options. Use `configureEnvironment` function the same way as `preprocessData`.
138
139```js
140nunjucks: {
141 options: {
142 configureEnvironment: function(env) {
143 // for instance, let's set a global variable across all templates
144 env.addGlobal('foo', 'bar');
145 }
146 },
147 render: {
148 files: [
149 {
150 expand: true,
151 cwd: "bundles/",
152 src: "*.html",
153 dest: "build/",
154 ext: ".html"
155 }
156 ]
157 }
158}
159```
160
161Check out [nunjucks' API](http://mozilla.github.io/nunjucks/api.html#environment) to know a list of available methods for environment object.
162
163Nice!