Javascript 如何在带有流星的车把助手中使用多个参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12558982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:42:33  来源:igfitidea点击:

How to use multiple parameters in a handlebar helper with meteor?

javascriptmeteorhandlebars.jsmeteor-helper

提问by Greg

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)

回答by Rui Gon?alves

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

玩得开心

回答by mad Man

Addition to

除了

<template name="myTemplate"> {{testHelper "value1" "value2"}} </template>

<template name="myTemplate"> {{testHelper "value1" "value2"}} </template>

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!!!!!

干杯!!!!!