/mustache-render

Grunt plugin for testing mustache templates

Primary LanguageJavaScriptMIT LicenseMIT

grunt-mustache-render v1.2.3

Build Status NPM version

This is a grunt plugin to render mustache templates. It takes data in JSON, YAML, or POJO (Plain Ol' JavaScript Object) format. It allows you to specify a folder for partials, instead of needing to list them individually.

Getting Started

This plugin requires Grunt ~0.4.1

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-mustache-render --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-mustache-render');

The "mustache_render" task

Overview

In your project's Gruntfile, add a section named mustache_render to the data object passed into grunt.initConfig().

grunt.initConfig({
  mustache_render: {
    options: {
      // Task global options go here
    },
    your_target: {
      options: {
        // Target specific options go here
      },
      files : [
        {
          data: // Path to JSON or YAML file, or POJO
          template: // Path to template file
          dest: // Path to output destination here
        }
      ]
    },
  },
})

Note: The files parameter must be an array, and must conform to the format specified above. Each object in the file array represents one rendered template. Data files can be in either JSON or YAML format or as a POJO (Plain Ol' JavaScript Object).

Examples:

files: [
  {data: "path/to/data/file.json",
   template: "path/to/template.mustache",
   dest: "file/to/output.html"}
]
files: [
  {data: { greeting: "Hello", target: "world" },
   template: "path/to/template.mustache",
   dest: "file/to/output.html"}
]

Options

options.directory

Type: String
Default value: ""

Path to the directory in which partials can be found. Partials are looked up by name in this directory.

options.extension

Type: String
Default value: ".mustache"

mustache-render will use this extension when looking up partials.

options.prefix

Type: String
Default value: ""

mustache-render will use this as a common prefix when looking up partials. So given the prefix: part_ for a partial named hello it will search for a file named part_hello.mustache.

options.clear_cache

Type: Boolean
Default value: false

Clears the mustache cache before running the target. Mustache will cache partials by name when running multiple tasks, so this option is usefull if options.extension, options.directory, or options.prefix have been changed between tasks.

Usage Examples

For this Grunt config:

grunt.initConfig({
  mustache_render: {
    all: {
      files: [{
        data: "data/hello_world.json",
        template: "templates/hello_world.mustache",
        dest: "public/hello_world.html"
      }]
    }
  }
})

And this json:

{
  "greeting" : "Hello",
  "target" : "World"
}

This template:

<html>
<head>
  <meta charset="UTF-8">
  <title>A greeting</title>
</head>
<body>
  <h1>{{greeting}}, {{target}}!</h1>
</body>
</html>

Will produce this output:

<html>
<head>
  <meta charset="UTF-8">
  <title>A greeting</title>
</head>
<body>
  <h1>Hello, World!</h1>
</body>
</html>

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

1.2.3

  • Accept YAML files with either .yaml or .yml extensions.

1.2.2

  • Major code refactor
  • Remove lodash dependency

1.2.1

  • Code Cleanup

1.2.0

  • Allow arbitrary JavaScript objects to be passed as data

1.1.0

  • Option for common prefix on partials
  • Option to clear mustache cache before running task

1.0.0

  • Created website
  • API now considered stable

0.3.0

  • Unverbosify options

0.2.1

  • Bugfix: dependencies

0.2.0

  • YAML data file support

0.1.0

  • Initial Release