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:
+13
-7
@@ -8,7 +8,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
|||||||
Compiler.OPCODE_MAP = {
|
Compiler.OPCODE_MAP = {
|
||||||
appendContent: 1,
|
appendContent: 1,
|
||||||
getContext: 2,
|
getContext: 2,
|
||||||
lookupWithFallback: 3,
|
lookupWithHelpers: 3,
|
||||||
lookup: 4,
|
lookup: 4,
|
||||||
append: 5,
|
append: 5,
|
||||||
invokeMustache: 6,
|
invokeMustache: 6,
|
||||||
@@ -25,7 +25,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
|||||||
Compiler.MULTI_PARAM_OPCODES = {
|
Compiler.MULTI_PARAM_OPCODES = {
|
||||||
appendContent: 1,
|
appendContent: 1,
|
||||||
getContext: 1,
|
getContext: 1,
|
||||||
lookupWithFallback: 1,
|
lookupWithHelpers: 1,
|
||||||
lookup: 1,
|
lookup: 1,
|
||||||
invokeMustache: 2,
|
invokeMustache: 2,
|
||||||
pushString: 1,
|
pushString: 1,
|
||||||
@@ -201,7 +201,7 @@ Handlebars.JavaScriptCompiler = function() {};
|
|||||||
|
|
||||||
this.opcode('getContext', id.depth);
|
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++) {
|
for(var i=1, l=id.parts.length; i<l; i++) {
|
||||||
this.opcode('lookup', id.parts[i]);
|
this.opcode('lookup', id.parts[i]);
|
||||||
@@ -432,11 +432,17 @@ Handlebars.JavaScriptCompiler = function() {};
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
lookupWithFallback: function(name) {
|
lookupWithHelpers: function(name) {
|
||||||
if(name) {
|
if(name) {
|
||||||
this.pushStack(this.nameLookup('currentContext', name, 'context'));
|
var topStack = this.nextStack();
|
||||||
var topStack = this.topStack();
|
|
||||||
this.source.push("if(" + topStack + " === undefined) { " + topStack + " = " + this.nameLookup('helpers', name, 'helper') + "; }");
|
var toPush = "if('" + name + "' in helpers) { " + topStack +
|
||||||
|
" = " + this.nameLookup('helpers', name, 'helper') +
|
||||||
|
"; } else { " + topStack + " = " +
|
||||||
|
this.nameLookup('currentContext', name, 'context') +
|
||||||
|
"; }";
|
||||||
|
|
||||||
|
this.source.push(toPush);
|
||||||
} else {
|
} else {
|
||||||
this.pushStack("currentContext");
|
this.pushStack("currentContext");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -632,3 +632,47 @@ test("you can override inherited data when invoking a helper with depth", functi
|
|||||||
equals("sad world?", result);
|
equals("sad world?", result);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("helpers take precedence over same-named context properties", function() {
|
||||||
|
var template = Handlebars.compile("{{goodbye}} {{cruel world}}");
|
||||||
|
|
||||||
|
var helpers = {
|
||||||
|
goodbye: function() {
|
||||||
|
return this.goodbye.toUpperCase();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var context = {
|
||||||
|
cruel: function(world) {
|
||||||
|
return "cruel " + world.toUpperCase();
|
||||||
|
},
|
||||||
|
|
||||||
|
goodbye: "goodbye",
|
||||||
|
world: "world"
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = template(context, helpers);
|
||||||
|
equals(result, "GOODBYE cruel WORLD");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("helpers take precedence over same-named context properties", function() {
|
||||||
|
var template = Handlebars.compile("{{#goodbye}} {{cruel world}}{{/goodbye}}");
|
||||||
|
|
||||||
|
var helpers = {
|
||||||
|
goodbye: function(fn) {
|
||||||
|
return this.goodbye.toUpperCase() + fn(this);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var context = {
|
||||||
|
cruel: function(world) {
|
||||||
|
return "cruel " + world.toUpperCase();
|
||||||
|
},
|
||||||
|
|
||||||
|
goodbye: "goodbye",
|
||||||
|
world: "world"
|
||||||
|
};
|
||||||
|
|
||||||
|
var result = template(context, helpers);
|
||||||
|
equals(result, "GOODBYE cruel WORLD");
|
||||||
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user