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

Add return statement to partials for AMD, issue #76 #146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,15 @@ module.exports = function(grunt) {
'tmp/amd_partials_no_namespace.js': ['test/fixtures/_partial.hbs', 'test/fixtures/one.hbs']
}
},
amd_partials_no_namespace_single_input: {
options: {
amd: ['handlebars'],
partialsUseNamespace: false
},
files: {
'tmp/amd_partials_no_namespace_single_input.js': ['test/fixtures/_partial.hbs']
}
},
amd_namespace_as_function: {
options: {
amd: ['handlebars', 'handlebars.helpers'],
Expand Down
2 changes: 1 addition & 1 deletion docs/handlebars-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ For this option to work you need to define the `namespace` option.
Type: `Boolean` or `String` or `Array` or `Function`
Default: `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.
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. Partial templates will be returned directly if they are the only input to the file.

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

Expand Down
12 changes: 10 additions & 2 deletions tasks/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,15 @@ module.exports = function(grunt) {
partials.push('Handlebars.registerPartial(' + JSON.stringify(filename) + ', ' + nsInfo.namespace +
'[' + JSON.stringify(filename) + '] = ' + compiled + ');');
} else {
partials.push('Handlebars.registerPartial(' + JSON.stringify(filename) + ', ' + compiled + ');');
if ((options.amd || options.commonjs) && f.src.length === 1) {
partials.push(
'var compiledPartial = ' + compiled + ';\n' +
'Handlebars.registerPartial(' + JSON.stringify(filename) + ', compiledPartial);\n' +
'return compiledPartial;'
);
} else {
partials.push('Handlebars.registerPartial(' + JSON.stringify(filename) + ', ' + compiled + ');');
}
}
} else {
if ((options.amd || options.commonjs) && !useNamespace) {
Expand Down Expand Up @@ -208,7 +216,7 @@ module.exports = function(grunt) {
output.unshift('define([' + amdString + '], function(Handlebars) {');
}

if (useNamespace) {
if (useNamespace && templates.length) {
// Namespace has not been explicitly set to false; the AMD
// wrapper will return the object containing the template.
output.push('return ' + extractGlobalNamespace(nsDeclarations) + ';');
Expand Down
9 changes: 9 additions & 0 deletions test/expected/amd_partials_no_namespace_single_input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
define(['handlebars'], function(Handlebars) {

var compiledPartial = Handlebars.template({"compiler":[6,">= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data) {
return "<span>Canada</span>";
},"useData":true});
Handlebars.registerPartial("partial", compiledPartial);
return compiledPartial;

});
9 changes: 9 additions & 0 deletions test/handlebars_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ exports.handlebars = {
test.done();
});
},
amd_partials_no_namespace_single_input: function(test) {
test.expect(1);

filesAreEqual('amd_partials_no_namespace_single_input.js', function(actual, expected) {
test.equal(actual, expected, 'should wrap partial with an AMD define block and no namespace and ' +
'return the partial template when the partial is the only input file.');
test.done();
});
},
amd_namespace_as_function: function(test) {
test.expect(1);

Expand Down