diff options
author | Yehuda Katz <wycats@gmail.com> | 2011-12-31 13:51:54 -0800 |
---|---|---|
committer | Yehuda Katz <wycats@gmail.com> | 2011-12-31 13:51:54 -0800 |
commit | 730c2617afa8da48ced07d8fba83152b3fae54d5 (patch) | |
tree | 181fe19e9ce23ac05884f5e394ed425ef98936df /lib/handlebars/compiler/compiler.js | |
parent | 757d5250dcd7cd8a979b0c5d92f2a1d24f12e663 (diff) | |
download | handlebars.js-730c2617afa8da48ced07d8fba83152b3fae54d5.zip handlebars.js-730c2617afa8da48ced07d8fba83152b3fae54d5.tar.gz handlebars.js-730c2617afa8da48ced07d8fba83152b3fae54d5.tar.bz2 |
Differentiate between lambdas in the context (which should have mustache semantics) and helpers (which have Handlebars helper semantics).
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; |