Giter VIP home page Giter VIP logo

grunt-underscore-compiler's Introduction

grunt-underscore-compiler

Compile underscore templates into JavaScript files.

Getting Started

This plugin requires Grunt ~0.4.5

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-underscore-compiler --save-dev

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

grunt.loadNpmTasks('grunt-underscore-compiler');

The "underscore" task

Overview

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

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

Options

options.separator

Type: String

Default value: linefeed + linefeed

Concatenated files will be joined on this string.

options.indent

Type: String

Default value:

By default 4 space indent is used for the generated code.

options.quoteChar

Type: String

Default value: '

Strings are quoted with single quote-only by default. However, for projects that want strict double-quotes usage, you can specify:

options: {
    quoteChar: '"'
}

options.raw

Type: Boolean

Default value: true

By default keep the HTML line break in the generated code. You can specify this option to false to get one line code, but you note that this option is not equal to htmlmin.

options.trim

Type: Boolean

Default value: true

By default trim each HTML line in the generated code, remove the space at the beginning and end for each line. You can specify this option to false to keep the space.

options.namespace

Type: String or false or Function

Default value: app.tpl

The namespace in which the precompiled templates will be assigned. Use dot notation (e.g. App.Templates) for nested namespaces or false for no namespace wrapping. When false with amd option set true, templates will be returned directly from the AMD wrapper.

Example:

options: {
  namespace: 'MyApp.Templates'
}

You can generate nested namespaces based on the file system paths of your templates by providing a function. The function will be called with one argument (the template filepath). The function must return a dot notation based on the filepath.

-The last array entry name derives from the processName option. The constructed final notation is <options.global>[<namespace>][<namespace>]...[<processName>]

Example:

options: {
  namespace: function(filename) {
    var names = filename.replace(/modules\/(.*)(\/\w+\.hbs)/, '$1');
    return names.split('/').join('.');
  },
  processName: function(filename) {
    return filename = filename.substring(filename.lastIndexOf('/') + 1, filename.lastIndexOf('.'));
  }
  demo: {
  	files: {
    	'ns_nested_tmpls.js' : [ 'modules/**/*.html']
  	}
  }
}

options.global

Type: String

Default value: this

The global object which the namespace belong to.

Example:

options: {
  namespace: 'MyApp.Templates',
  global: 'this'
}

The generated code will like:

this[MyApp][Templates][SomeName] = 'your template';

options.amd

Type: String or Boolean or Array

Default value: false

Wraps the output file with an AMD define function and returns the compiled template namespace unless namespace has been explicitly set to false in which case the template function will be returned directly.

If String then that string will be used in the module definition define(['your_amd_opt_here']).

If Array then those strings will be used in the module definition.

options.compile

Type: Boolean

Default value: false

By default return the HTML string. Set to true will get precompiled template function.

options.templateSettings

Type: Object

Default value: {}

underscore's templateSettings.

Usage Examples

grunt.initConfig({
  underscore: {
    options: {
        namespace: 'MyApp.Templates'
    },
    demo:{
      files: {
        'path/to/result.js': 'path/to/source.html',
        'path/to/another.js': ["path/to/sources/*.html', 'path/to/more/*.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

  • 2014-11-22 v0.0.1 Compile underscore templates into JavaScript files.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.