Passing a parameter into Meteor Template Helper

1.1k views Asked by At

I am trying to pass a parameter to a template helper. The parameter seems to be passed but then I can't use it as I would like to.

I pass the parameter like this:

{{#if unitFieldExists 'profile'}}
  {{profile}}
{{/}}

Helper in /client/lib/helpers.js

Template.registerHelper('unitFieldExists', function(unitField) {
var doc = Units.findOne({_id: this._id }, { unitField : {$exists: true} });

// console tests
console.log(unitField); // Outputs profile
console.log(doc); // Outputs document object

// unitfield if of type string
console.log(typeof unitField + " " + unitField); // string

if (doc.unitField) {
    return true;
} else {
    return false;
}
});

What I am trying to achieve is to return true to the #if if the document contains the field passed. I am probably over complicating things but I am still learning.

1

There are 1 answers

2
Peter Sutton On BEST ANSWER

You cannot call helpers with arguments in an #if.

I assume that the template instance's data context is a Unit document because of your use of this._id. If so, can just do;

{{#if profile}}
  {{profile}}
{{/if}}

The #if checks whether its argument is truthy.