diff options
Diffstat (limited to 'lib/handlebars/compiler/compiler.js')
-rw-r--r-- | lib/handlebars/compiler/compiler.js | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/lib/handlebars/compiler/compiler.js b/lib/handlebars/compiler/compiler.js index 4bc1f82..b322063 100644 --- a/lib/handlebars/compiler/compiler.js +++ b/lib/handlebars/compiler/compiler.js @@ -408,6 +408,12 @@ Handlebars.JavaScriptCompiler = function() {}; preamble: function() { var out = []; + // this register will disambiguate helper lookup from finding a function in + // a context. This is necessary for mustache compatibility, which requires + // that context functions in blocks are evaluated by blockHelperMissing, and + // then proceed as if the resulting value was provided to blockHelperMissing. + this.useRegister('foundHelper'); + if (!this.isChild) { var namespace = this.namespace; var copies = "helpers = helpers || " + namespace + ".helpers;"; @@ -520,10 +526,8 @@ Handlebars.JavaScriptCompiler = function() {}; } else if (isScoped || this.options.knownHelpersOnly) { toPush = topStack + " = " + this.nameLookup('depth' + this.lastContext, name, 'context'); } else { - toPush = topStack + " = " - + this.nameLookup('helpers', name, 'helper') - + " || " - + this.nameLookup('depth' + this.lastContext, name, 'context'); + this.register('foundHelper', this.nameLookup('helpers', name, 'helper')); + toPush = topStack + " = foundHelper || " + this.nameLookup('depth' + this.lastContext, name, 'context'); } toPush += ';'; @@ -618,10 +622,10 @@ Handlebars.JavaScriptCompiler = function() {}; params.push(stringOptions); - this.populateCall(params, id, helperId || id, fn); + this.populateCall(params, id, helperId || id, fn, program !== '{}'); }, - populateCall: function(params, id, helperId, fn) { + populateCall: function(params, id, helperId, fn, program) { var paramString = ["depth0"].concat(params).join(", "); var helperMissingString = ["depth0"].concat(helperId).concat(params).join(", "); @@ -631,7 +635,8 @@ Handlebars.JavaScriptCompiler = function() {}; this.source.push(nextStack + " = " + id + ".call(" + paramString + ");"); } else { this.context.aliases.functionType = '"function"'; - this.source.push("if(typeof " + id + " === functionType) { " + nextStack + " = " + id + ".call(" + paramString + "); }"); + var condition = program ? "foundHelper && " : "" + this.source.push("if(" + condition + "typeof " + id + " === functionType) { " + nextStack + " = " + id + ".call(" + paramString + "); }"); } fn.call(this, nextStack, helperMissingString, id); this.usingKnownHelper = false; |