Spacebars.kw
包含一个hash
具有输入参数哈希的对象。
Meteor有两种匹配方式,一种是直接匹配,即直接输入参数,例如{{testHelper "variable1" "variable2" "variable3"}}
,将匹配为function(a,b,c)
作为变量 1-3 分别匹配 a、b 和 c。
第二种输入方法是使用hash:
{{testHelper a="variable1" b="variable2" c="variable3"}}
这将为function(a)
其中 a 是Spacebars.kw
object.
The Spacebars.kw
对象将有一个名为的子对象hash
具有匹配的结构:
{ "a" : "variable1",
"b" : "variable2",
"c" : "variable3" }
Meteor 将尝试直接匹配第一个参数,但后续参数将作为哈希值进行匹配,以防第二个输入为空,例如您使用{{testHelper 'hello'}}
where b
将为空,因此它以散列形式给出。
它通常是这样给出的,所以如果你得到 b 作为 aSpacebars.kw
对象,您可以假设没有第二个输入。另一种方法是您可以使用哈希样式声明,然后直接检查哈希值是否为null
:
{{testHelper text="Hello"}}
{{testHelper text="Hello" othertext="Hellooo"}}
和助手:
Template.registerHelper('testHelper', function(kw) {
console.log(kw.hash.text);
console.log(kw.hash.othertext);
});