ghusse
ghusse

Reputation: 3210

I cannot get Handlebars helpers to work with arguments in meteorjs

I'm trying to use a simple handlebars helper, with arguments, in MeteorJS.

Let me show you an example:

{{#myList data className="myClassName"}}
  {{name}}
{{/myList}}

The helper is declared this way:

Handlebars.registerHelper('myList', function(context, options) {
  if (context && context.length){
    var className = options.hash.className || "", 
       ret = "<ul class='"+ className +"'>";

    for(var i=0, j=context.length; i<j; i++) {
      ret = ret + options.fn(context[i]);
    }

    return ret + "</ul>";
  }
});

But the property "hash" is ALWAYS an empty array.

If I test a version like this one:

{{#myList data "myClassName"}}
  {{name}}
{{/myList}}

The callback method never receive the second argument.

Is there something I'm doing wrong?

Upvotes: 2

Views: 700

Answers (1)

Xyand
Xyand

Reputation: 4488

From meteor/handlebars:

In Meteor, block helpers do not take arbtirary positional and keyword arguments like non-block helpers. Instead, the arguments are treated together as a nested Handlebars helper invocation expression.

But you should be able to pass only keyword arguments:

The exact rule is that a block helper is always invoked with either no arguments; one positional argument (and no keyword arguments); or only keyword arguments. The presence of any non-keyword argument, like foo in the previous example, causes all following positional and keyword arguments to be passed to foo (if it is a function, or else swallowed). Otherwise, if there are only keyword arguments, they are passed to the helper, so you can define a block helper that takes any number of arguments by giving them names: {{#helper x=1 y=2 z=3}}...{{/helper}}.

This should work:

{{#myList myData=data className="myClassName"}}
  {{name}}
{{/myList}}

Upvotes: 2

Related Questions