Helpers take precedence over context properties with the same name. This is useful in scenarios where your context object is inherited from another system (such as a framework or JSON API) that may contain properties that conflict with helpers you explicitly define.

This commit is contained in:
tomhuda
2011-02-25 23:01:11 -08:00
parent 6fbdf4b445
commit 32d7e52182
2 changed files with 57 additions and 7 deletions
+13 -7
View File
@@ -8,7 +8,7 @@ Handlebars.JavaScriptCompiler = function() {};
Compiler.OPCODE_MAP = {
appendContent: 1,
getContext: 2,
lookupWithFallback: 3,
lookupWithHelpers: 3,
lookup: 4,
append: 5,
invokeMustache: 6,
@@ -25,7 +25,7 @@ Handlebars.JavaScriptCompiler = function() {};
Compiler.MULTI_PARAM_OPCODES = {
appendContent: 1,
getContext: 1,
lookupWithFallback: 1,
lookupWithHelpers: 1,
lookup: 1,
invokeMustache: 2,
pushString: 1,
@@ -201,7 +201,7 @@ Handlebars.JavaScriptCompiler = function() {};
this.opcode('getContext', id.depth);
this.opcode('lookupWithFallback', id.parts[0] || null);
this.opcode('lookupWithHelpers', id.parts[0] || null);
for(var i=1, l=id.parts.length; i<l; i++) {
this.opcode('lookup', id.parts[i]);
@@ -432,11 +432,17 @@ Handlebars.JavaScriptCompiler = function() {};
}
},
lookupWithFallback: function(name) {
lookupWithHelpers: function(name) {
if(name) {
this.pushStack(this.nameLookup('currentContext', name, 'context'));
var topStack = this.topStack();
this.source.push("if(" + topStack + " === undefined) { " + topStack + " = " + this.nameLookup('helpers', name, 'helper') + "; }");
var topStack = this.nextStack();
var toPush = "if('" + name + "' in helpers) { " + topStack +
" = " + this.nameLookup('helpers', name, 'helper') +
"; } else { " + topStack + " = " +
this.nameLookup('currentContext', name, 'context') +
"; }";
this.source.push(toPush);
} else {
this.pushStack("currentContext");
}