Pass Argument To Helper in Template.Dynamic Call

I have a situation where I need to load a dynamic template, and I need my helper to have a parameter so that I can load the proper template name from Mongo.

<div id="sw">
        {{> Template.dynamic template=getPanel }}
</div>

So, I would like to pass ‘sw’ to the getPanel helper, so that I can fetch the appropriate panel from the database and return it’s name to the Template.dynamic call. Here is what I have:

getPanel: function() {
    result = Panel.findOne({ owner: Meteor.userId()});
    return result.panelName;
}

but this is what I need (psuedocode)

getPanel: function(direction) {
    result = Panel.findOne({ owner: Meteor.userId(), direction: direction });
    return result.panelName;
}

Is it possible to pass data to the getPanel helper from the {{> Template.dynamic … }} call?

1 Like

What you want is something like this: {{> Template.dynamic template=(getPanel argument) }}. This is not supported right now. Maybe in the future (see here).

Possible workaround:

<div id="sw">
  {{#with direction=33}}
    {{> Template.dynamic template=getPanel }}
  {{/with}}
</div>

getPanel: function() {
    result = Panel.findOne({ owner: Meteor.userId(), direction: this.direction });
    return result.panelName;
}
2 Likes

Ah, very helpful workaround!

To use it in a normal template helper (not a dynamic one), I used Template.parentData().direction in the template helper.

1 Like

So, there’s some great news I just found out about. As of Meteor v1.2 (Sept 2015), Handlebars sub-expressions are supported.

As in,

{{helper (anotherHelper arg1 arg2)}}

I don’t know about you guys, but I was pretty thrilled to discover this development. Hats off to whoever worked on this!

2 Likes