How to pass multiple arguments to Spacebars helper from Meteor template?

11.2k views Asked by At

I haven't been able to find a solid example out there.

Template.registerHelper("itemLookup", function(sku, property){
  return Items.findOne({sku: sku})[property];
});

How do I call this on the template?

I want to do something like:

{{ itemLookup sku="i3_4030U" property="title" }}

It should output

"Intel Core i3 4030U"
1

There are 1 answers

7
saimeunt On BEST ANSWER

Do not name template helpers parameters, they will be passed in the same order to your helper function :

{{ itemLookup "i3_4030U" "title" }}

EDIT :

Why then do I see examples online where they are naming the template helper parameters?

You can name parameters when including another template and you want to set its current data context to something else :

{{> childTemplate param1="A" param2="B"}}

In the child template markup you'll be able to reference {{param1}} and {{param2}}.

Another Handlebars helpers feature available in Spacebars is the "hash" optional argument value you can pass as the last argument to your helper parameters, you can use it like this :

HTML

{{helper "A" "B" namedParam1="C" namedParam2="D"}}

JS

Template.registerHelper("helper", function(param1, param2, options){
  console.log("param1 :", param1);
  console.log("param2 :", param2);
  if(options && options.hash){
    console.log("namedParam1 :", options.hash.namedParam1);
    console.log("namedParam2 :", options.hash.namedParam2);
  }
});