Handlebars 子表达式抛出 "options.fn is not a function" 错误
Handlebars subexpression throws "options.fn is not a function" error
我试图在 Handlebars 中使用子表达式,但即使在最简单的表达式上也会出现 "options.fn is not a function" 错误。在使用来自 https://github.com/assemble/handlebars-helpers 的额外助手时,此表达式工作正常:
{{#and true true}}OK{{/and}}
但是如果我像这样创建一个子表达式
{{#and (gt 4 3) (gt 5 4)}}OK{{/and}}
或者这个
{{#and (gt 4 3) true}}OK{{/and}}
库抛出错误
TypeError: [feed.hbs] options.fn is not a function
at Object.helpers.gt (/Users/me/Projects/jackal/node_modules/handlebars-helpers/lib/comparison.js:152:20)
at Object.eval (eval at createFunctionContext ...
我需要检查两个条件。这时用嵌套表达式实现了:
{{#gt 4 3}}
{{#gt 5 4}}
ok
{{/gt}}
{{/gt}}
那么我的子表达式有什么问题?
在我看来 子表达式 不被 handlebars-helpers
支持。
我用调试器快速查看了代码。对于 {{#and (gt 4 3) (gt 5 4)}}OK{{/and}}
和 (gt 4 3)
本身被正确调用,但是 gt
助手的代码是:
helpers.gt = function(a, b, options) {
if (arguments.length === 2) {
options = b;
b = options.hash.compare;
}
if (a > b) {
return options.fn(this);
}
return options.inverse(this);
};
但是因为子表达式既没有 fn
(if 块),也没有 inverse
(else 块),handlebars-helpers
此时失败。
为了支持您的表达,handlebars-helpers
需要 - 恕我直言 - 将他们的代码重写为类似的东西:
helpers.gt = function(a, b, options) {
if (arguments.length === 2) {
options = b;
b = options.hash.compare;
}
//fn block exists to it is not a subexpression
if( options.fn ) {
if (a > b) {
return options.fn(this);
}
return options.inverse(this);
} else {
return a > b;
}
};
所以现在你不能使用带有 handlebars-helpers
的子表达式。
我在他们的 github 页面上添加了一个问题:Supporting Handlebars subexpressions
我试图在 Handlebars 中使用子表达式,但即使在最简单的表达式上也会出现 "options.fn is not a function" 错误。在使用来自 https://github.com/assemble/handlebars-helpers 的额外助手时,此表达式工作正常:
{{#and true true}}OK{{/and}}
但是如果我像这样创建一个子表达式
{{#and (gt 4 3) (gt 5 4)}}OK{{/and}}
或者这个
{{#and (gt 4 3) true}}OK{{/and}}
库抛出错误
TypeError: [feed.hbs] options.fn is not a function
at Object.helpers.gt (/Users/me/Projects/jackal/node_modules/handlebars-helpers/lib/comparison.js:152:20)
at Object.eval (eval at createFunctionContext ...
我需要检查两个条件。这时用嵌套表达式实现了:
{{#gt 4 3}}
{{#gt 5 4}}
ok
{{/gt}}
{{/gt}}
那么我的子表达式有什么问题?
在我看来 子表达式 不被 handlebars-helpers
支持。
我用调试器快速查看了代码。对于 {{#and (gt 4 3) (gt 5 4)}}OK{{/and}}
和 (gt 4 3)
本身被正确调用,但是 gt
助手的代码是:
helpers.gt = function(a, b, options) {
if (arguments.length === 2) {
options = b;
b = options.hash.compare;
}
if (a > b) {
return options.fn(this);
}
return options.inverse(this);
};
但是因为子表达式既没有 fn
(if 块),也没有 inverse
(else 块),handlebars-helpers
此时失败。
为了支持您的表达,handlebars-helpers
需要 - 恕我直言 - 将他们的代码重写为类似的东西:
helpers.gt = function(a, b, options) {
if (arguments.length === 2) {
options = b;
b = options.hash.compare;
}
//fn block exists to it is not a subexpression
if( options.fn ) {
if (a > b) {
return options.fn(this);
}
return options.inverse(this);
} else {
return a > b;
}
};
所以现在你不能使用带有 handlebars-helpers
的子表达式。
我在他们的 github 页面上添加了一个问题:Supporting Handlebars subexpressions