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

孤街醉人 提交于 2019-12-03 06:13:24

问题


I am trying to create a custom helper using Meteor. Following to the doc here: 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:

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').

Meteor version 0.4.0 (8f4045c1b9)


回答1:


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.

If you want to register testHelper globally you'll need to do something like this

Handlebars.registerHelper('testHelper', function(foo, bar){
  console.log(foo);
  console.log(bar);
});

Have fun




回答2:


Addition to

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



来源:https://stackoverflow.com/questions/12558982/how-to-use-multiple-parameters-in-a-handlebar-helper-with-meteor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!