我正在尝试在 MeteorJS 中使用带有参数的简单 Handlebars 助手。
让我给你举个例子:
{{#myList data className="myClassName"}}
{{name}}
{{/myList}}
助手是这样声明的:
Handlebars.registerHelper('myList', function(context, options) {
if (context && context.length){
var className = options.hash.className || "",
ret = "<ul class='"+ className +"'>";
for(var i=0, j=context.length; i<j; i++) {
ret = ret + options.fn(context[i]);
}
return ret + "</ul>";
}
});
但是属性“hash”始终是一个空数组。
如果我测试这样的版本:
{{#myList data "myClassName"}}
{{name}}
{{/myList}}
回调方法永远不会收到第二个参数。
我是不是做错了什么?
最佳答案
In Meteor, block helpers do not take arbtirary positional and keyword arguments like non-block helpers. Instead, the arguments are treated together as a nested Handlebars helper invocation expression.
但是您应该能够仅传递关键字参数:
The exact rule is that a block helper is always invoked with either no arguments; one positional argument (and no keyword arguments); or only keyword arguments. The presence of any non-keyword argument, like foo in the previous example, causes all following positional and keyword arguments to be passed to foo (if it is a function, or else swallowed). Otherwise, if there are only keyword arguments, they are passed to the helper, so you can define a block helper that takes any number of arguments by giving them names: {{#helper x=1 y=2 z=3}}...{{/helper}}.
这应该有效:
{{#myList myData=data className="myClassName"}}
{{name}}
{{/myList}}
关于javascript - 我无法让 Handlebars 助手使用meteorjs 中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17746786/