grunt-nunjucks-2-html

Grunt task for rendering nunjucks` templates to HTML

npm install grunt-nunjucks-2-html
13 downloads in the last week
29 downloads in the last month

Grunt task for rendering nunjucks` templates to HTML

NPM version

Getting start

If you haven't used Grunt before, be sure to check out the Getting Started guide.

Once plugin has been installed include it in your Gruntfile.js

grunt.loadNpmTasks('grunt-nunjucks-2-html');

Usage examples

Task targets and options may be specified according to the grunt Configuring tasks guide.

nunjucks: {
  options: {
    data: grunt.file.readJSON('data.json')
  },
  render: {
    files: {
      'index.html' : 'templates/index-template.html'
    }
  }
}

index.html is now compiled with data.json!

nunjucks: {
  options: {
    data: grunt.file.readJSON('data.json')
  },
  render: {
    files: [
       {
          expand: true,
          cwd: "bundles/",
          src: "*.html",
          dest: "build/",
          ext: ".html"
       }
    ]
  }

You'll get a set of html files in build folder.

Options

data

Read JSON from file using grunt.file.readJSON or specify object just inside your Gruntfile.

preprocessData

You 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. If you specify a data option it would be passed inside the function as an argument.

For instance, you could include name of the file inside an every data object


var path = require('path');

...

nunjucks: {
  options: {
    preprocessData: function(data) {
      var page = path.basename(this.src[0], '.html');
      var result = {
        page: page,
        data: data
      };
      return result;
    },
    data: grunt.file.readJSON('data.json')
  },
  render: {
    files: [
       {
          expand: true,
          cwd: "bundles/",
          src: "*.html",
          dest: "build/",
          ext: ".html"
       }
    ]
  }

Nice!

npm loves you