From 8699b506cfe402bf5e4875725abc6b56d7edcce9 Mon Sep 17 00:00:00 2001 From: Tyler Kellen Date: Wed, 12 Sep 2012 10:52:39 -0500 Subject: [PATCH] add processName function - closes issue gh-1 --- README.md | 12 ++++++++++++ tasks/jst.js | 53 ++++++++++++++++++++++++++-------------------------- 2 files changed, 38 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index d7f7183..e34021e 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,18 @@ This controls how this task (and its helpers) operate and should contain key:val The namespace in which the resulting JST templates are assigned to. +##### processName ```function``` + +This option accepts a function which takes one argument (the template filepath) and returns a string which will be used as the key for the precompiled template object. The example below stores all templates on the default JST namespace in capital letters. + +``` javascript +options: { + processName: function(filename) { + return filename.toUpperCase(); + } +} +``` + ##### templateSettings ```object``` The settings passed to underscore when compiling templates. diff --git a/tasks/jst.js b/tasks/jst.js index ca06b1b..eaa4a76 100644 --- a/tasks/jst.js +++ b/tasks/jst.js @@ -14,51 +14,50 @@ module.exports = function(grunt) { var escapeQuote = function(name) { return name.replace("'","\\'"); }; - var jst = function(source, filepath, namespace, templateSettings) { - try { - return namespace + "['" + escapeQuote(filepath) + "'] = " + _.template(source, false, templateSettings).source + ";"; - } catch (e) { - grunt.log.error(e); - grunt.fail.warn("JST failed to compile."); - } - }; + // filename conversion for templates + var defaultProcessName = function(name) { return name; }; grunt.registerMultiTask("jst", "Compile underscore templates to JST file", function() { var helpers = require("grunt-contrib-lib").init(grunt); var options = helpers.options(this, {namespace: "JST", templateSettings: {}}); + // assign filename transformation functions + var processName = options.processName || defaultProcessName; + grunt.verbose.writeflags(options, "Options"); // TODO: ditch this when grunt v0.4 is released this.files = this.files || helpers.normalizeMultiTaskFiles(this.data, this.target); - var srcFiles; - var taskOutput; - var sourceCode; - var sourceCompiled; + var compiled, srcFiles, src, filename; + var output = []; + var namespace = "this['" + options.namespace + "']"; - var helperNamespace = "this['" + options.namespace + "']"; + this.files.forEach(function(files) { + srcFiles = grunt.file.expandFiles(files.src); + srcFiles.forEach(function(file) { + src = grunt.file.read(file); - this.files.forEach(function(file) { - srcFiles = grunt.file.expandFiles(file.src); + try { + compiled = _.template(src, false, options.templateSettings).source; + } catch (e) { + grunt.log.error(e); + grunt.fail.warn("JST failed to compile."); + } - taskOutput = []; - taskOutput.push(helperNamespace + " = " + helperNamespace + " || {};"); - - srcFiles.forEach(function(srcFile) { - sourceCode = grunt.file.read(srcFile); - - sourceCompiled = jst(sourceCode, srcFile, helperNamespace, options.templateSettings); - - taskOutput.push(sourceCompiled); + filename = escapeQuote(processName(file)); + output.push(namespace+"['"+filename+"'] = "+compiled+";"); }); - if (taskOutput.length > 0) { - grunt.file.write(file.dest, taskOutput.join("\n\n")); - grunt.log.writeln("File '" + file.dest + "' created."); + if(output.length > 0) { + output.unshift(namespace + " = " + namespace + " || {};"); + grunt.file.write(files.dest, output.join("\n\n")); + grunt.log.writeln("File '" + files.dest + "' created."); } }); + + }); };