Fixed an issue where {{#array}} {{/array}} wouldn't pass in an

@index data variable.

I just thought it would be nice to add in this feature. I'm using
the two of these a little interchangable, so if other people are doing
that as well, it might help usability.
This commit is contained in:
Karl Westin
2012-08-27 12:55:55 -07:00
parent 89f5ab8aaf
commit acc04c2826
2 changed files with 12 additions and 5 deletions
+2 -5
View File
@@ -44,13 +44,10 @@ Handlebars.registerHelper('blockHelperMissing', function(context, options) {
return inverse(this);
} else if(type === "[object Array]") {
if(context.length > 0) {
for(var i=0, j=context.length; i<j; i++) {
ret = ret + fn(context[i]);
}
return Handlebars.helpers.each(context, options);
} else {
ret = inverse(this);
return inverse(this);
}
return ret;
} else {
return fn(context);
}
+10
View File
@@ -234,6 +234,16 @@ test("array", function() {
});
test("array with @index", function() {
var string = "{{#goodbyes}}{{@index}}. {{text}}! {{/goodbyes}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};
var template = CompilerContext.compile(string);
var result = template(hash);
equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used");
});
test("empty block", function() {
var string = "{{#goodbyes}}{{/goodbyes}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};