Skip to content

Commit

Permalink
Add @FIRST and @last data variables to #each helper resolving Issue h…
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskuczynski committed Sep 21, 2013
1 parent 0fe78f3 commit 849e80a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/handlebars/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ Handlebars.registerHelper('each', function(context, options) {
if(context && typeof context === 'object') {
if (isArray(context)) {
for(var j = context.length; i<j; i++) {
if (data) { data.index = i; }
if (data) {
data.index = i;
data.first = (i === 0) ? true : false;
data.last = (i === (context.length-1)) ? true : false;
}
ret = ret + fn(context[i], { data: data });
}
} else {
Expand Down
40 changes: 40 additions & 0 deletions spec/builtins.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,46 @@ describe('builtin helpers', function() {
equal(result, "0. goodbye! 0 1 2 After 0 1. Goodbye! 0 1 2 After 1 2. GOODBYE! 0 1 2 After 2 cruel world!", "The @index variable is used");
});

it("each with @first", function() {
var string = "{{#each goodbyes}}{{#if @first}}{{text}}! {{/if}}{{/each}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};

var template = CompilerContext.compile(string);
var result = template(hash);

equal(result, "goodbye! cruel world!", "The @first variable is used");
});

it("each with nested @first", function() {
var string = "{{#each goodbyes}}({{#if @first}}{{text}}! {{/if}}{{#each ../goodbyes}}{{#if @first}}{{text}}!{{/if}}{{/each}}{{#if @first}} {{text}}!{{/if}}) {{/each}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};

var template = CompilerContext.compile(string);
var result = template(hash);

equal(result, "(goodbye! goodbye! goodbye!) (goodbye!) (goodbye!) cruel world!", "The @first variable is used");
});

it("each with @last", function() {
var string = "{{#each goodbyes}}{{#if @last}}{{text}}! {{/if}}{{/each}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};

var template = CompilerContext.compile(string);
var result = template(hash);

equal(result, "GOODBYE! cruel world!", "The @last variable is used");
});

it("each with nested @last", function() {
var string = "{{#each goodbyes}}({{#if @last}}{{text}}! {{/if}}{{#each ../goodbyes}}{{#if @last}}{{text}}!{{/if}}{{/each}}{{#if @last}} {{text}}!{{/if}}) {{/each}}cruel {{world}}!";
var hash = {goodbyes: [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}], world: "world"};

var template = CompilerContext.compile(string);
var result = template(hash);

equal(result, "(GOODBYE!) (GOODBYE!) (GOODBYE! GOODBYE! GOODBYE!) cruel world!", "The @last variable is used");
});

it("each with function argument", function() {
var string = "{{#each goodbyes}}{{text}}! {{/each}}cruel {{world}}!";
var hash = {goodbyes: function () { return [{text: "goodbye"}, {text: "Goodbye"}, {text: "GOODBYE"}];}, world: "world"};
Expand Down

0 comments on commit 849e80a

Please sign in to comment.