I am trying to create a custom helper using Meteor. Following to the doc here: https://github.com/meteor/meteor/wiki/Handlebars
我正在尝试使用Meteor创建自定义帮助程序。关于这里的文档:https://github.com/meteor/meteor/wiki/Handlebars
I have tried to define my helper as follows:
我试图按如下方式定义我的助手:
Template.myTemplate.testHelper = function(foo, bar, options) {
console.log(foo);
console.log(bar);
}
My template looks like:
我的模板看起来像:
<template name="myTemplate">
{{#testHelper "value1" "value2"}}
{{/testHelper}}
</template>
Looking at my console output, I expected to see 2 lines of output:
看看我的控制台输出,我希望看到2行输出:
value1
value2
However my console looks like:
但是我的控制台看起来像:
value1
function (data) {
// don't create spurious annotations when data is same
// as before (or when transitioning between e.g. `window` and
// `undefined`)
if ((data || Handlebars._defaultThis) ===
(old_data || Handlebars._defaultThis))
return fn(data);
else
return Spark.setDataContext(data, fn(data));
}
Note, I am completely new to meteor, and to handlebars. I think I would be much happier using underscore, but the documentation for meteor glances over underscore almost entirely. Am I doing something wrong defining my helper function? It seems that it is not seeing the second parameter "bar", and instead interpreting that as the options. (Note: if I console.log(options) it returns 'undefined').
请注意,我对流星和车把都是全新的。我想使用下划线我会更开心,但流星的文档几乎完全瞥了一眼下划线。我在定义辅助函数时做错了吗?它似乎没有看到第二个参数“bar”,而是将其解释为选项。 (注意:如果我是console.log(options),则返回'undefined')。
Meteor version 0.4.0 (8f4045c1b9)
流星版0.4.0(8f4045c1b9)
2 个解决方案
#1
53
Your logic is good, just make some changes to the template
您的逻辑很好,只需对模板进行一些更改即可
<template name="myTemplate">
{{testHelper "value1" "value2"}}
</template>
Bare in mind that the testHelper function is only defined in the myTemplate template.
请记住,testHelper函数仅在myTemplate模板中定义。
If you want to register testHelper globally you'll need to do something like this
如果你想在全球注册testHelper,你需要做这样的事情
Handlebars.registerHelper('testHelper', function(foo, bar){
console.log(foo);
console.log(bar);
});
Have fun
玩的开心
#2
1
Addition to
除了
<template name="myTemplate"> {{testHelper "value1" "value2"}} </template>
{{testHelper“value1”“value2”}}
Instead of passing a value as a parameter pass the function as parameter Here is the code for that
而不是将值作为参数传递将函数作为参数传递。以下是该代码的代码
<template name="myTemplate">
{{ testHelper1 (testHelper2 "value2") }}
</template>
cheers!!!!!
干杯!!!!!
#1
53
Your logic is good, just make some changes to the template
您的逻辑很好,只需对模板进行一些更改即可
<template name="myTemplate">
{{testHelper "value1" "value2"}}
</template>
Bare in mind that the testHelper function is only defined in the myTemplate template.
请记住,testHelper函数仅在myTemplate模板中定义。
If you want to register testHelper globally you'll need to do something like this
如果你想在全球注册testHelper,你需要做这样的事情
Handlebars.registerHelper('testHelper', function(foo, bar){
console.log(foo);
console.log(bar);
});
Have fun
玩的开心
#2
1
Addition to
除了
<template name="myTemplate"> {{testHelper "value1" "value2"}} </template>
{{testHelper“value1”“value2”}}
Instead of passing a value as a parameter pass the function as parameter Here is the code for that
而不是将值作为参数传递将函数作为参数传递。以下是该代码的代码
<template name="myTemplate">
{{ testHelper1 (testHelper2 "value2") }}
</template>
cheers!!!!!
干杯!!!!!