grunt-simple-templates

0.1.0 • Public • Published

grunt-simple-templates

Simple compilation of JavaScript templates into objects

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-simple-templates --save-dev

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

grunt.loadNpmTasks('grunt-simple-templates');

The "templates" task

Overview

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

grunt.initConfig({
  templates: {
    options: {
      // Task-specific options go here.
    },
    your_target: {
      // Target-specific file lists and/or options go here.
    },
  },
})

Options

options.namespace

Type: String Default value: 'TEMPLATES'

Your templates will be in an object called window.#{NAMESPACE}.

options.extension

Type: String Default value: 'mustache'

Templates matching the extension options.extension will be compiled.

Usage Examples

Default Options

In this example, the default options are used to compile Mustache templates. So if the testing.mustache file has the content {{test}} and the dir/testing.mustache file had the content {{nested test}}, the generated result would be window.TEMPLATES = {"testing":"{{test}}\n","dir/testing":"{{nested test}}\n"};

grunt.initConfig({
  templates: {
    options: {},
    files: {
      src: 'templates/',
      dest: 'js/templates.js'
    },
  },
})

Custom Options

In this example, custom options are used to compile Handlebars templates. So if the testing.hbs file has the content {{test}} and the dir/testing.hbs file had the content {{nested test}}, the generated result in this case would be window.HANDLEBARS = {"testing":"{{test}}\n","dir/testing":"{{nested test}}\n"};

grunt.initConfig({
  templates: {
    options: {
      namespace: 'HANDLEBARS',
      extension: 'hbs'
    },
    files: {
      src: 'templates/',
      dest: 'js/templtes.js'
    },
  },
})

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

0.0.2: Only match provided or default extension.

0.0.1: Initial release.

Readme

Keywords

none

Package Sidebar

Install

npm i grunt-simple-templates

Weekly Downloads

1

Version

0.1.0

License

none

Last publish

Collaborators

  • jclem