Javascript 如何在meteor的车把辅助工具中使用多个参数?

Javascript 如何在meteor的车把辅助工具中使用多个参数?,javascript,meteor,handlebars.js,meteor-helper,Javascript,Meteor,Handlebars.js,Meteor Helper,我正在尝试使用Meteor创建一个自定义助手。以下是本文件的附件: 我已尝试按如下方式定义我的助手: Template.myTemplate.testHelper = function(foo, bar, options) { console.log(foo); console.log(bar); } 我的模板看起来像: <template name="myTemplate"> {{#testHelper "value1" "value2"}} {

我正在尝试使用Meteor创建一个自定义助手。以下是本文件的附件:

我已尝试按如下方式定义我的助手:

Template.myTemplate.testHelper = function(foo, bar, options) {
    console.log(foo);
    console.log(bar);
}
我的模板看起来像:

<template name="myTemplate">
    {{#testHelper "value1" "value2"}}
    {{/testHelper}}
</template>
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));
  } 
但是,我的控制台看起来像:

<template name="myTemplate">
    {{#testHelper "value1" "value2"}}
    {{/testHelper}}
</template>
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));
  } 
请注意,我对流星和车把是完全陌生的。我想我会更乐意使用下划线,但meteor的文档几乎完全忽略了下划线。我是否在定义助手函数时出错?它似乎没有看到第二个参数“bar”,而是将其解释为选项。(注意:如果I console.log(options)返回'undefined')


Meteor版本0.4.0(8f4045c1b9)

您的逻辑很好,只需对模板进行一些更改即可

<template name="myTemplate">
  {{testHelper "value1" "value2"}}
</template>
玩得开心

除了


{{testHelper“value1”“value2”}

这里的代码不是将值作为参数传递,而是将函数作为参数传递

<template name="myTemplate">
    {{ testHelper1 (testHelper2 "value2") }}
</template>

{{testHelper1(testHelper2“value2”)}

干杯

如果您需要{{{if}}内容{{/if}样式的帮助器,则此选项无效。在这种情况下,可以组合帮助器:
{{{if testHelper“v1”“v2”}某些模板{/if}
应该把
把手.registerHelper
放在哪里?@KarlMorrison–你可以把它放在运行客户端代码的任何地方,例如,我通常也把它放在
myApp/client/lib/helpers/template helpers.js中,对于较新版本的Meteor,您可能希望使用以下语法分别注册每个助手:
Template.registerHelper('myHelperName',function(arg1,arg2){[…]})。您不需要将此代码包装成任何内容,只需将其作为单个代码段放入template-helper.js中,每个代码段之间都有大量空格,以便于阅读。;)