Skip to content

Commit

Permalink
Add preventIndent option for partials
Browse files Browse the repository at this point in the history
This disables the standalone partial indent behavior required by the Mustache spec and allows for users to utilize partials in the same manner as under 1.x.

Fixes #858
  • Loading branch information
kpdecker committed Nov 2, 2014
1 parent 7c220b9 commit 632fadc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ Compiler.prototype = {
this.opcode('pushContext');
}

this.opcode('invokePartial', partialName.name, partial.indent || '');
var indent = partial.indent || '';
if (this.options.preventIndent && indent) {
this.opcode('appendContent', indent);
indent = '';
}
this.opcode('invokePartial', partialName.name, indent);
this.opcode('append');
},

Expand Down
2 changes: 1 addition & 1 deletion spec/env/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ global.compileWithPartials = function(string, hashOrArray, partials) {
ary = [];
ary.push(hashOrArray[0]);
ary.push({ helpers: hashOrArray[1], partials: hashOrArray[2] });
options = {compat: hashOrArray[3]};
options = typeof hashOrArray[3] === 'object' ? hashOrArray[3] : {compat: hashOrArray[3]};
if (hashOrArray[4] != null) {
options.data = !!hashOrArray[4];
ary[1].data = hashOrArray[4];
Expand Down
8 changes: 8 additions & 0 deletions spec/partials.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ describe('partials', function() {
shouldCompileToWithPartials(string, [hash, {}, {dude: dude, url: url}], true,
"Dudes:\n Yehuda\n http://yehuda!\n Alan\n http://alan!\n");
});
it("prevent nested indented partials", function() {
var string = "Dudes:\n{{#dudes}}\n {{>dude}}\n{{/dudes}}";
var dude = "{{name}}\n {{> url}}";
var url = "{{url}}!\n";
var hash = {dudes: [{name: "Yehuda", url: "http://yehuda"}, {name: "Alan", url: "http://alan"}]};
shouldCompileToWithPartials(string, [hash, {}, {dude: dude, url: url}, {preventIndent: true}], true,
"Dudes:\n Yehuda\n http://yehuda!\n Alan\n http://alan!\n");

This comment has been minimized.

Copy link
@joshterrell805

joshterrell805 Nov 3, 2014

Excellent, thank you!

});
});

describe('compat mode', function() {
Expand Down

0 comments on commit 632fadc

Please sign in to comment.