Pass Argument To Helper in Template.Dynamic Call


#1

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?


#2

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;
}

#3

Ah, very helpful workaround!


#4

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


#5

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!