Allow helperMissing to apply to simple mustaches (paves the way to support things like link_to in a Rails context)
This commit is contained in:
+1
-1
@@ -54,7 +54,7 @@ Handlebars.registerPartial = function(name, str) {
|
|||||||
this.partials[name] = str;
|
this.partials[name] = str;
|
||||||
};
|
};
|
||||||
|
|
||||||
Handlebars.registerHelper('helperMissing', function(context, fn) {
|
Handlebars.registerHelper('blockHelperMissing', function(context, fn) {
|
||||||
var ret = "";
|
var ret = "";
|
||||||
|
|
||||||
if(context === true) {
|
if(context === true) {
|
||||||
|
|||||||
@@ -113,11 +113,22 @@ Handlebars.Runtime.prototype = {
|
|||||||
params[i] = this[param.type](param).data;
|
params[i] = this[param.type](param).data;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(toString.call(idObj.data) === "[object Function]") {
|
var data = idObj.data;
|
||||||
buf = idObj.data.apply(this.wrapContext(), params);
|
|
||||||
|
var type = toString.call(data);
|
||||||
|
var functionType = (type === "[object Function]");
|
||||||
|
|
||||||
|
if(!functionType && params.length) {
|
||||||
|
params = params.slice(0);
|
||||||
|
params.unshift(data || mustache.id.original);
|
||||||
|
data = this.context.helpers.helperMissing;
|
||||||
|
functionType = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(functionType) {
|
||||||
|
buf = data.apply(this.wrapContext(), params);
|
||||||
} else {
|
} else {
|
||||||
if(params.length) { throw new Error(mustache.id.parts.join("/") + " is not a Function, so you cannot have Function parameters"); }
|
buf = data;
|
||||||
buf = idObj.data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(buf && mustache.escaped) { buf = Handlebars.Utils.escapeExpression(buf); }
|
if(buf && mustache.escaped) { buf = Handlebars.Utils.escapeExpression(buf); }
|
||||||
@@ -136,7 +147,7 @@ Handlebars.Runtime.prototype = {
|
|||||||
|
|
||||||
if(toString.call(data) !== "[object Function]") {
|
if(toString.call(data) !== "[object Function]") {
|
||||||
params = [data];
|
params = [data];
|
||||||
data = this.context.helpers.helperMissing;
|
data = this.context.helpers.blockHelperMissing;
|
||||||
} else {
|
} else {
|
||||||
params = this.evaluateParams(mustache.params);
|
params = this.evaluateParams(mustache.params);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
module("basic context");
|
module("basic context");
|
||||||
|
|
||||||
|
Handlebars.registerHelper('helperMissing', function(helper, context) {
|
||||||
|
if(helper === "link_to") {
|
||||||
|
return new Handlebars.SafeString("<a>" + context + "</a>");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
var shouldCompileTo = function(string, hash, expected, message) {
|
var shouldCompileTo = function(string, hash, expected, message) {
|
||||||
var template = Handlebars.compile(string);
|
var template = Handlebars.compile(string);
|
||||||
if(Object.prototype.toString.call(hash) === "[object Array]") {
|
if(Object.prototype.toString.call(hash) === "[object Array]") {
|
||||||
@@ -459,3 +465,11 @@ test("constructing a safestring from a string and checking its type", function()
|
|||||||
equal(safe, "testing 1, 2, 3", "SafeString is equivalent to its underlying string");
|
equal(safe, "testing 1, 2, 3", "SafeString is equivalent to its underlying string");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
module("helperMissing");
|
||||||
|
|
||||||
|
test("if a context is not found, helperMissing is used", function() {
|
||||||
|
var string = "{{hello}} {{link_to world}}"
|
||||||
|
var context = { hello: "Hello", world: "world" };
|
||||||
|
|
||||||
|
shouldCompileTo(string, context, "Hello <a>world</a>")
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user