Skip to content

Compile + invoke templates immediately? #35

@cowboy

Description

@cowboy

There are 2 main use-cases I see for "javascript" templates:

  1. Compile template files into JavaScript functions for use on the client. These templates will be namespaced and combined into a single .js file.
  2. Compile template files into JavaScript functions for use on the server. These templates will be run immediately, with the output being saved into .html files.

This plugin already does 1, but what about 2? In my example, I'm already using lo-dash templates in the client via requirejs plugin, but I don't want to use Jade for the app's index.html page. I also want to use lo-dash templates.

So, in my project Gruntfile, I created this "tmpl" task, but it seems a little redundant for this to be a separate thing.

grunt.registerMultiTask('tmpl', 'compile lodash templates to html files', function() {
  var _ = require('lodash');
  var options = this.options({
    data: {},
    templateSettings: null,
  });
  var origTemplateSettings = _.templateSettings;
  this.files.forEach(function(f) {
    var src = grunt.file.read(f.src[0]);
    if (options.templateSettings) {
      _.templateSettings = _.extend(_.templateSettings, options.templateSettings);
    }
    var html = _.template(src, options.data);
    grunt.file.write(f.dest, html);
    _.templateSettings = origTemplateSettings;
  });
});

Is there a place in grunt-contrib-jst for the "2" behavior?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions