summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2013-05-29 12:23:55 -0400
committerkpdecker <kpdecker@gmail.com>2013-05-29 12:23:55 -0400
commitc540d7186adb28db3cbd2923cbf82d60f44f777e (patch)
tree358654084c35339595cb1472027f8a9860f18e45
parent293672432b3912a1d1f6b659a7e08334532f56d5 (diff)
downloadhandlebars.js-c540d7186adb28db3cbd2923cbf82d60f44f777e.zip
handlebars.js-c540d7186adb28db3cbd2923cbf82d60f44f777e.tar.gz
handlebars.js-c540d7186adb28db3cbd2923cbf82d60f44f777e.tar.bz2
Allow function arguments to with and each
Via @mcdan Fixes #239
-rw-r--r--dist/handlebars.js6
-rw-r--r--dist/handlebars.runtime.js6
-rw-r--r--lib/handlebars/base.js6
-rw-r--r--spec/qunit_spec.js13
4 files changed, 31 insertions, 0 deletions
diff --git a/dist/handlebars.js b/dist/handlebars.js
index 5139dc8..95da9e4 100644
--- a/dist/handlebars.js
+++ b/dist/handlebars.js
@@ -124,6 +124,9 @@ Handlebars.registerHelper('each', function(context, options) {
var fn = options.fn, inverse = options.inverse;
var i = 0, ret = "", data;
+ var type = toString.call(context);
+ if(type === functionType) { context = context.call(this); }
+
if (options.data) {
data = Handlebars.createFrame(options.data);
}
@@ -168,6 +171,9 @@ Handlebars.registerHelper('unless', function(conditional, options) {
});
Handlebars.registerHelper('with', function(context, options) {
+ var type = toString.call(context);
+ if(type === functionType) { context = context.call(this); }
+
if (!Handlebars.Utils.isEmpty(context)) return options.fn(context);
});
diff --git a/dist/handlebars.runtime.js b/dist/handlebars.runtime.js
index ac2b020..d11c79d 100644
--- a/dist/handlebars.runtime.js
+++ b/dist/handlebars.runtime.js
@@ -124,6 +124,9 @@ Handlebars.registerHelper('each', function(context, options) {
var fn = options.fn, inverse = options.inverse;
var i = 0, ret = "", data;
+ var type = toString.call(context);
+ if(type === functionType) { context = context.call(this); }
+
if (options.data) {
data = Handlebars.createFrame(options.data);
}
@@ -168,6 +171,9 @@ Handlebars.registerHelper('unless', function(conditional, options) {
});
Handlebars.registerHelper('with', function(context, options) {
+ var type = toString.call(context);
+ if(type === functionType) { context = context.call(this); }
+
if (!Handlebars.Utils.isEmpty(context)) return options.fn(context);
});
diff --git a/lib/handlebars/base.js b/lib/handlebars/base.js
index d7284b0..ad2d9ec 100644
--- a/lib/handlebars/base.js
+++ b/lib/handlebars/base.js
@@ -101,6 +101,9 @@ Handlebars.registerHelper('each', function(context, options) {
var fn = options.fn, inverse = options.inverse;
var i = 0, ret = "", data;
+ var type = toString.call(context);
+ if(type === functionType) { context = context.call(this); }
+
if (options.data) {
data = Handlebars.createFrame(options.data);
}
@@ -145,6 +148,9 @@ Handlebars.registerHelper('unless', function(conditional, options) {
});
Handlebars.registerHelper('with', function(context, options) {
+ var type = toString.call(context);
+ if(type === functionType) { context = context.call(this); }
+
if (!Handlebars.Utils.isEmpty(context)) return options.fn(context);
});
diff --git a/spec/qunit_spec.js b/spec/qunit_spec.js
index 6a093f3..31c9b96 100644
--- a/spec/qunit_spec.js
+++ b/spec/qunit_spec.js
@@ -797,6 +797,10 @@ test("with", function() {
var string = "{{#with person}}{{first}} {{last}}{{/with}}";
shouldCompileTo(string, {person: {first: "Alan", last: "Johnson"}}, "Alan Johnson");
});
+test("with with function argument", function() {
+ var string = "{{#with person}}{{first}} {{last}}{{/with}}";
+ shouldCompileTo(string, {person: function() { return {first: "Alan", last: "Johnson"};}}, "Alan Johnson");
+});
test("if", function() {
var string = "{{#if goodbye}}GOODBYE {{/if}}cruel {{world}}!";
@@ -861,6 +865,15 @@ test("each with @index", function() {
equal(result, "0. goodbye! 1. Goodbye! 2. GOODBYE! cruel world!", "The @index variable is used");
});
+test("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"};
+ shouldCompileTo(string, hash, "goodbye! Goodbye! GOODBYE! cruel world!",
+ "each with array function argument iterates over the contents when not empty");
+ shouldCompileTo(string, {goodbyes: [], world: "world"}, "cruel world!",
+ "each with array function argument ignores the contents when empty");
+});
+
test("data passed to helpers", function() {
var string = "{{#each letters}}{{this}}{{detectDataInsideEach}}{{/each}}";
var hash = {letters: ['a', 'b', 'c']};