UNPKG

2.17 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 },
24 render: {
25 files: {
26 'index.html' : 'templates/index-template.html'
27 }
28 }
29}
30```
31
32`index.html` is now compiled with `data.json`!
33
34```javascipt
35nunjucks: {
36 options: {
37 data: grunt.file.readJSON('data.json')
38 },
39 render: {
40 files: [
41 {
42 expand: true,
43 cwd: "bundles/",
44 src: "*.html",
45 dest: "build/",
46 ext: ".html"
47 }
48 ]
49 }
50```
51
52You'll get a set of html files in `build` folder.
53
54## Options
55
56### data
57
58Read JSON from file using `grunt.file.readJSON` or specify object just inside your `Gruntfile`.
59
60### preprocessData
61
62You 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.
63
64For instance, you could include name of the file inside an every data object
65
66```javascipt
67
68var path = require('path');
69
70...
71
72nunjucks: {
73 options: {
74 preprocessData: function(data) {
75 var page = path.basename(this.src[0], '.html');
76 var result = {
77 page: page,
78 data: data
79 };
80 return result;
81 },
82 data: grunt.file.readJSON('data.json')
83 },
84 render: {
85 files: [
86 {
87 expand: true,
88 cwd: "bundles/",
89 src: "*.html",
90 dest: "build/",
91 ext: ".html"
92 }
93 ]
94 }
95```
96
97Nice!
\No newline at end of file