summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordenniskuczynski <dennis.kuczynski@gmail.com>2013-09-21 15:45:43 -0400
committerkpdecker <kpdecker@gmail.com>2013-10-12 16:43:17 -0500
commite20591b49dbc38b391fa9dfc3846e0bf9dd9cfff (patch)
treedbdde0e72fc5864b3d6fbd383ea476946eec1271
parent583141de7cb61eb70eaa6b33c25f475f3048071b (diff)
downloadhandlebars.js-e20591b49dbc38b391fa9dfc3846e0bf9dd9cfff.zip
handlebars.js-e20591b49dbc38b391fa9dfc3846e0bf9dd9cfff.tar.gz
handlebars.js-e20591b49dbc38b391fa9dfc3846e0bf9dd9cfff.tar.bz2
Add @first and @last data variables to #each helper resolving Issue #483
-rw-r--r--lib/handlebars/base.js6
-rw-r--r--spec/builtins.js40
2 files changed, 45 insertions, 1 deletions
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js
index 29c8de8..cf0cbb9 100644
--- a/lib/handlebars/base.js
+++ b/lib/handlebars/base.js
@@ -106,7 +106,11 @@ function registerDefaultHelpers(instance) {
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 {
diff --git a/spec/builtins.js b/spec/builtins.js
index c678964..140f5f5 100644
--- a/spec/builtins.js
+++ b/spec/builtins.js
@@ -95,6 +95,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"};