Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds options.handlebarsNamespace #96

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,23 @@ module.exports = function(grunt) {
files: {
'tmp/namespace_as_function.js' : [ 'test/fixtures/modules/**/*.hbs']
}
},
handlebars_namespace: {
options: {
handlebarsNamespace: 'customNamespace'
},
files: {
'tmp/handlebars_namespace.js': ['test/fixtures/_partial.hbs', 'test/fixtures/one.hbs']
}
},
handlebars_namespace_amd: {
options: {
handlebarsNamespace: 'customNamespace',
amd: true
},
files: {
'tmp/handlebars_namespace_amd.js': ['test/fixtures/_partial.hbs', 'test/fixtures/one.hbs']
}
}
},
// Unit tests.
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ options: {
}
```

#### handlebarsNamespace
Type: `String`
Default: `''`

The namespace of the handlebars object. Use dot notation (e.g. myObject.myProp) for nested namespaces. Primarily useful for browser environments in which handlebars isn't attached to the global object.

Example:
```js
options: {
handlesbarsNamespace: 'someObject.someProperty'
}
```

#### partialsUseNamespace
Type: `Boolean`
Default: `false`
Expand Down Expand Up @@ -271,4 +284,4 @@ handlebars: {

Task submitted by [Tim Branyen](http://tbranyen.com)

*This file was generated on Thu Jan 23 2014 09:22:44.*
*This file was generated on Thu Feb 20 2014 20:31:05.*
13 changes: 13 additions & 0 deletions docs/handlebars-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ options: {
}
```

## handlebarsNamespace
Type: `String`
Default: `''`

The namespace of the handlebars object. Use dot notation (e.g. myObject.myProp) for nested namespaces. Primarily useful for browser environments in which handlebars isn't attached to the global object.

Example:
```js
options: {
handlesbarsNamespace: 'someObject.someProperty'
}
```

## partialsUseNamespace
Type: `Boolean`
Default: `false`
Expand Down
18 changes: 15 additions & 3 deletions tasks/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ module.exports = function(grunt) {
var processPartialName = options.processPartialName || defaultProcessPartialName;
var processAST = options.processAST || defaultProcessAST;
var useNamespace = options.namespace !== false;
var hbsNs = options.handlebarsNamespace || '';

// Only set the handlebars namespace in strictly browser environments
if (options.amd || options.node || options.commonjs) {
grunt.log.warn('handlebarsNamespace can only be used in a strictly browser environment. The option has been ignored.');
hbsNs = '';
} else {
// append a . to the handlebars namespace if it isn't there
hbsNs = (hbsNs === '' || hbsNs.slice(-1) === '.') ? hbsNs : hbsNs+'.';
}


var namespaceInfo = _.memoize(function(filepath) {
if (!useNamespace) {return undefined;}
Expand Down Expand Up @@ -101,7 +112,7 @@ module.exports = function(grunt) {

// if configured to, wrap template in Handlebars.template call
if (options.wrapped === true) {
compiled = 'Handlebars.template('+compiled+')';
compiled = hbsNs+'Handlebars.template('+compiled+')';
}
} catch (e) {
grunt.log.error(e);
Expand All @@ -112,14 +123,15 @@ module.exports = function(grunt) {
if (partialsPathRegex.test(filepath) && isPartial.test(_.last(filepath.split('/')))) {
filename = processPartialName(filepath);
if (options.partialsUseNamespace === true) {
partials.push('Handlebars.registerPartial('+JSON.stringify(filename)+', '+nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+');');
partials.push(hbsNs+'Handlebars.registerPartial('+JSON.stringify(filename)+', '+nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+');');
} else {
partials.push('Handlebars.registerPartial('+JSON.stringify(filename)+', '+compiled+');');
partials.push(hbsNs+'Handlebars.registerPartial('+JSON.stringify(filename)+', '+compiled+');');
}
} else {
if(options.amd && !useNamespace) {
compiled = 'return ' + compiled;
}

filename = processName(filepath);
if (useNamespace) {
templates.push(nsInfo.namespace+'['+JSON.stringify(filename)+'] = '+compiled+';');
Expand Down
27 changes: 27 additions & 0 deletions test/expected/handlebars_namespace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
this["JST"] = this["JST"] || {};

customNamespace.Handlebars.registerPartial("partial", customNamespace.Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};



return "<span>Canada</span>";
}));

this["JST"]["test/fixtures/one.hbs"] = customNamespace.Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); partials = this.merge(partials, Handlebars.partials); data = data || {};
var buffer = "", stack1, functionType="function", escapeExpression=this.escapeExpression, self=this;


buffer += "<p>Hello, my name is ";
if (stack1 = helpers.name) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
else { stack1 = (depth0 && depth0.name); stack1 = typeof stack1 === functionType ? stack1.call(depth0, {hash:{},data:data}) : stack1; }
buffer += escapeExpression(stack1)
+ ". I live in ";
stack1 = self.invokePartial(partials.partial, 'partial', depth0, helpers, partials, data);
if(stack1 || stack1 === 0) { buffer += stack1; }
buffer += "</p>";
return buffer;
});
33 changes: 33 additions & 0 deletions test/expected/handlebars_namespace_amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
define(['handlebars'], function(Handlebars) {

this["JST"] = this["JST"] || {};

Handlebars.registerPartial("partial", Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};



return "<span>Canada</span>";
}));

this["JST"]["test/fixtures/one.hbs"] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); partials = this.merge(partials, Handlebars.partials); data = data || {};
var buffer = "", stack1, functionType="function", escapeExpression=this.escapeExpression, self=this;


buffer += "<p>Hello, my name is ";
if (stack1 = helpers.name) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
else { stack1 = (depth0 && depth0.name); stack1 = typeof stack1 === functionType ? stack1.call(depth0, {hash:{},data:data}) : stack1; }
buffer += escapeExpression(stack1)
+ ". I live in ";
stack1 = self.invokePartial(partials.partial, 'partial', depth0, helpers, partials, data);
if(stack1 || stack1 === 0) { buffer += stack1; }
buffer += "</p>";
return buffer;
});

return this["JST"];

});
16 changes: 16 additions & 0 deletions test/handlebars_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,22 @@ exports.handlebars = {
test.equal(actual, expected, 'namespace should allow function to allow nested namespaces based on file system structure.');
test.done();
});
},
handlebars_namespace: function(test) {
test.expect(1);

filesAreEqual('handlebars_namespace.js', function(actual, expected) {
test.equal(actual, expected, 'should support specifying a namespace for handlebars.');
test.done();
});
},
handlebars_namespace_amd: function(test) {
test.expect(1);

filesAreEqual('handlebars_namespace_amd.js', function(actual, expected) {
test.equal(actual, expected, 'should ignore the handlebars namespace for non-browser environments.');
test.done();
});
}

};