summaryrefslogtreecommitdiffstats
path: root/spec/builtins.js
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 /spec/builtins.js
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
Diffstat (limited to 'spec/builtins.js')
-rw-r--r--spec/builtins.js40
1 files changed, 40 insertions, 0 deletions
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"};