Merge pull request #3 from lazd/nstmpl
Flexible namespaces for templates
This commit is contained in:
commit
6b5f9b1944
@ -37,7 +37,14 @@ This controls how this task (and its helpers) operate and should contain key:val
|
||||
|
||||
##### namespace ```string```
|
||||
|
||||
The namespace in which the resulting JST templates are assigned to.
|
||||
The namespace, in dot notation, in which the resulting JST templates are assigned to. The example below assigns templates to the `MyApp.Templates` namespace:
|
||||
|
||||
Example:
|
||||
``` javascript
|
||||
options: {
|
||||
namespace: 'MyApp.Templates'
|
||||
}
|
||||
```
|
||||
|
||||
##### processName ```function```
|
||||
|
||||
|
20
grunt.js
20
grunt.js
@ -42,12 +42,28 @@ module.exports = function(grunt) {
|
||||
jst: {
|
||||
compile: {
|
||||
files: {
|
||||
"tmp/jst.js": ["test/fixtures/*.html"]
|
||||
"tmp/jst.js": ["test/fixtures/template.html"]
|
||||
}
|
||||
},
|
||||
uglyfile: {
|
||||
files: {
|
||||
"tmp/uglyfile.js": ["test/fixtures/*bad*"]
|
||||
"tmp/uglyfile.js": ["test/fixtures/*bad-filename*"]
|
||||
}
|
||||
},
|
||||
ns_nested: {
|
||||
options: {
|
||||
namespace: "MyApp.JST.Main"
|
||||
},
|
||||
files: {
|
||||
"tmp/ns_nested.js": ["test/fixtures/template.html"]
|
||||
}
|
||||
},
|
||||
ns_nested_this: {
|
||||
options: {
|
||||
namespace: "this.MyApp.JST.Main"
|
||||
},
|
||||
files: {
|
||||
"tmp/ns_nested_this.js": ["test/fixtures/template.html"]
|
||||
}
|
||||
}
|
||||
},
|
||||
|
11
tasks/jst.js
11
tasks/jst.js
@ -11,8 +11,7 @@ module.exports = function(grunt) {
|
||||
"use strict";
|
||||
|
||||
var _ = require("underscore");
|
||||
|
||||
var escapeQuote = function(name) { return name.replace("'","\\'"); };
|
||||
var helpers = require('grunt-contrib-lib').init(grunt);
|
||||
|
||||
// filename conversion for templates
|
||||
var defaultProcessName = function(name) { return name; };
|
||||
@ -32,7 +31,7 @@ module.exports = function(grunt) {
|
||||
|
||||
var compiled, srcFiles, src, filename;
|
||||
var output = [];
|
||||
var namespace = "this['" + options.namespace + "']";
|
||||
var nsInfo = helpers.getNamespaceDeclaration(options.namespace);
|
||||
|
||||
this.files.forEach(function(files) {
|
||||
srcFiles = grunt.file.expandFiles(files.src);
|
||||
@ -46,12 +45,12 @@ module.exports = function(grunt) {
|
||||
grunt.fail.warn("JST failed to compile.");
|
||||
}
|
||||
|
||||
filename = escapeQuote(processName(file));
|
||||
output.push(namespace+"['"+filename+"'] = "+compiled+";");
|
||||
filename = processName(file);
|
||||
output.push(nsInfo.namespace+"["+JSON.stringify(filename)+"] = "+compiled+";");
|
||||
});
|
||||
|
||||
if(output.length > 0) {
|
||||
output.unshift(namespace + " = " + namespace + " || {};");
|
||||
output.unshift(nsInfo.declaration);
|
||||
grunt.file.write(files.dest, output.join("\n\n"));
|
||||
grunt.log.writeln("File '" + files.dest + "' created.");
|
||||
}
|
||||
|
11
test/expected/jst.js
Normal file
11
test/expected/jst.js
Normal file
@ -0,0 +1,11 @@
|
||||
this["JST"] = this["JST"] || {};
|
||||
|
||||
this["JST"]["test/fixtures/template.html"] = function(obj){
|
||||
var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};
|
||||
with(obj||{}){
|
||||
__p+='<head><title>'+
|
||||
( title )+
|
||||
'</title></head>';
|
||||
}
|
||||
return __p;
|
||||
};
|
13
test/expected/ns_nested.js
Normal file
13
test/expected/ns_nested.js
Normal file
@ -0,0 +1,13 @@
|
||||
this["MyApp"] = this["MyApp"] || {};
|
||||
this["MyApp"]["JST"] = this["MyApp"]["JST"] || {};
|
||||
this["MyApp"]["JST"]["Main"] = this["MyApp"]["JST"]["Main"] || {};
|
||||
|
||||
this["MyApp"]["JST"]["Main"]["test/fixtures/template.html"] = function(obj){
|
||||
var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};
|
||||
with(obj||{}){
|
||||
__p+='<head><title>'+
|
||||
( title )+
|
||||
'</title></head>';
|
||||
}
|
||||
return __p;
|
||||
};
|
17
test/expected/uglyfile.js
Normal file
17
test/expected/uglyfile.js
Normal file
@ -0,0 +1,17 @@
|
||||
this["JST"] = this["JST"] || {};
|
||||
|
||||
this["JST"]["test/fixtures/it\"s-a-bad-filename.html"] = function(obj){
|
||||
var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};
|
||||
with(obj||{}){
|
||||
__p+='never name your file like this.';
|
||||
}
|
||||
return __p;
|
||||
};
|
||||
|
||||
this["JST"]["test/fixtures/it's-a-bad-filename.html"] = function(obj){
|
||||
var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};
|
||||
with(obj||{}){
|
||||
__p+='never name your file like this.';
|
||||
}
|
||||
return __p;
|
||||
};
|
1
test/fixtures/it"s-a-bad-filename.html
vendored
Normal file
1
test/fixtures/it"s-a-bad-filename.html
vendored
Normal file
@ -0,0 +1 @@
|
||||
never name your file like this.
|
@ -6,16 +6,24 @@ exports['jst'] = {
|
||||
|
||||
var expect, result;
|
||||
|
||||
test.expect(2);
|
||||
test.expect(4);
|
||||
|
||||
expect = 'this[\'JST\'] = this[\'JST\'] || {};\n\nthis[\'JST\'][\'test/fixtures/it\\\'s-a-bad-filename.html\'] = function(obj){\nvar __p=\'\';var print=function(){__p+=Array.prototype.join.call(arguments, \'\')};\nwith(obj||{}){\n__p+=\'never name your file like this.\';\n}\nreturn __p;\n};\n\nthis[\'JST\'][\'test/fixtures/template.html\'] = function(obj){\nvar __p=\'\';var print=function(){__p+=Array.prototype.join.call(arguments, \'\')};\nwith(obj||{}){\n__p+=\'<head><title>\'+\n( title )+\n\'</title></head>\';\n}\nreturn __p;\n};';
|
||||
expect = grunt.file.read("test/expected/jst.js");
|
||||
result = grunt.file.read("tmp/jst.js");
|
||||
test.equal(expect, result, "should compile underscore templates into JST");
|
||||
|
||||
expect = 'this[\'JST\'] = this[\'JST\'] || {};\n\nthis[\'JST\'][\'test/fixtures/it\\\'s-a-bad-filename.html\'] = function(obj){\nvar __p=\'\';var print=function(){__p+=Array.prototype.join.call(arguments, \'\')};\nwith(obj||{}){\n__p+=\'never name your file like this.\';\n}\nreturn __p;\n};';
|
||||
expect = grunt.file.read("test/expected/uglyfile.js");
|
||||
result = grunt.file.read("tmp/uglyfile.js");
|
||||
test.equal(expect, result, "should escape single quotes in filenames");
|
||||
|
||||
expect = grunt.file.read("test/expected/ns_nested.js");
|
||||
result = grunt.file.read("tmp/ns_nested.js");
|
||||
test.equal(expect, result, "should define parts of nested namespaces");
|
||||
|
||||
expect = grunt.file.read("test/expected/ns_nested.js"); // same as previous test
|
||||
result = grunt.file.read("tmp/ns_nested_this.js");
|
||||
test.equal(expect, result, "should define parts of nested namespaces, ignoring this.");
|
||||
|
||||
test.done();
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user