summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2014-08-23 18:26:35 -0500
committerkpdecker <kpdecker@gmail.com>2014-08-23 18:26:57 -0500
commit0c7c37df6aa31a3e70f9eecadcaaa25928921c94 (patch)
treea21305079851aab7330e5d9a41a79f3c4f5bbf0a
parentcde008b49f3cc64711a23c107fa53ad612954aef (diff)
downloadhandlebars.js-0c7c37df6aa31a3e70f9eecadcaaa25928921c94.zip
handlebars.js-0c7c37df6aa31a3e70f9eecadcaaa25928921c94.tar.gz
handlebars.js-0c7c37df6aa31a3e70f9eecadcaaa25928921c94.tar.bz2
Restore helperMissing for ambiguous statements
Fixes #318 (regression) Partial fix for #783
-rw-r--r--lib/handlebars/compiler/javascript-compiler.js3
-rw-r--r--spec/helpers.js15
2 files changed, 17 insertions, 1 deletions
diff --git a/lib/handlebars/compiler/javascript-compiler.js b/lib/handlebars/compiler/javascript-compiler.js
index e53b4f2..c59fd70 100644
--- a/lib/handlebars/compiler/javascript-compiler.js
+++ b/lib/handlebars/compiler/javascript-compiler.js
@@ -551,6 +551,7 @@ JavaScriptCompiler.prototype = {
// `knownHelpersOnly` flags at compile-time.
invokeAmbiguous: function(name, helperCall) {
this.aliases.functionType = '"function"';
+ this.aliases.helperMissing = 'helpers.helperMissing';
this.useRegister('helper');
var nonHelper = this.popStack();
@@ -561,7 +562,7 @@ JavaScriptCompiler.prototype = {
var helperName = this.lastHelper = this.nameLookup('helpers', name, 'helper');
this.push(
- '((helper = ' + helperName + ' || ' + nonHelper
+ '((helper = (helper = ' + helperName + ' || ' + nonHelper + ') != null ? helper : helperMissing'
+ (helper.paramsInit ? '),(' + helper.paramsInit : '') + '),'
+ '(typeof helper === functionType ? helper.call(' + helper.callParams + ') : helper))');
},
diff --git a/spec/helpers.js b/spec/helpers.js
index e3b5863..e604e91 100644
--- a/spec/helpers.js
+++ b/spec/helpers.js
@@ -440,6 +440,21 @@ describe('helpers', function() {
shouldCompileTo(string, [context, helpers], "Hello <a>world</a>");
});
+
+ it("if a value is not found, custom helperMissing is used", function() {
+ var string = "{{hello}} {{link_to}}";
+ var context = { hello: "Hello", world: "world" };
+
+ var helpers = {
+ helperMissing: function(options) {
+ if(options.name === "link_to") {
+ return new Handlebars.SafeString("<a>winning</a>");
+ }
+ }
+ };
+
+ shouldCompileTo(string, [context, helpers], "Hello <a>winning</a>");
+ });
});
describe("knownHelpers", function() {