我正在尝试在Handlebars中使用子表达式,但是即使在最简单的表达式上也出现“ options.fn不是函数”错误。在使用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不是 Object.eval中的
Object.helpers.gt(/Users/me/Projects/jackal/node_modules/handlebars-helpers/lib/comparison.js:152:20)
中的函数(等效于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页面上添加了一个Issue:Supporting Handlebars子表达式
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句