npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-nunjucks-2-html

v2.2.0

Published

Grunt task for rendering nunjucks` templates to HTML

Downloads

325

Readme

Grunt task for rendering nunjucks` templates to HTML

NPM version Build Status

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'),
    paths: 'templates'
  },
  render: {
    files: {
      'index.html' : ['index.html']
    }
  }
}

templates/index.html (relative to the gruntfile) 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.

Tests

$ npm test

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

nunjucks: {
  options: {
    preprocessData: function(data) {
      var page = require('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"
       }
    ]
  }
}

paths

You could specify root path for your templates, paths would be set for nunjucks' configure

configureEnvironment

You could use nunjucks' environment API to set some global options. Use configureEnvironment function the same way as preprocessData.

As the second argument for the function you have nunjucks` instance, so you can do some extra work before rendering. For instance, you can pre-render some string in custom filter or extension.

nunjucks: {
  options: {
    configureEnvironment: function(env, nunjucks) {
      // for instance, let's set a global variable across all templates
      env.addGlobal('foo', 'bar');
    }
  },
  render: {
    files: [
       {
          expand: true,
          cwd: "bundles/",
          src: "*.html",
          dest: "build/",
          ext: ".html"
       }
    ]
  }
}

Check out nunjucks' API to know a list of available methods for environment object.

Nunjucks' configure API

You can use nunjucks' configure API as options for plugin.

tags

If you want different tokens than {{ and the rest for variables, blocks, and comments, you can specify different tokens as the tags option:

nunjucks: {
  options: {
    tags: {
      blockStart: '<%',
      blockEnd: '%>',
      variableStart: '<$',
      variableEnd: '$>',
      commentStart: '<#',
      commentEnd: '#>'
    },
    data: grunt.file.readJSON('data.json')
  },
  render: {
    files: [
       {
          expand: true,
          cwd: "bundles/",
          src: "*.html",
          dest: "build/",
          ext: ".html"
       }
    ]
  }
}

autoescape

By default, nunjucks escapes all output. Details

throwOnUndefined

Throw errors when outputting a null/undefined value

trimBlocks

Automatically remove trailing newlines from a block/tag

lstripBlocks

Automatically remove leading whitespace from a block/tag