JoeTidee
JoeTidee

Reputation: 26044

Using a template function in a conditional statement

I have a template helper function that converts my Mongo _id fields as a string:

Template.registerHelper('formatMongoId', function(data) {
    return (data && data._str) || data;
});

I want to use it in a conditional statement within a template:

{{#if $eq box_group_id formatMongoId ../_id._str}}
    ....
{{/if}}

but this is not working - any ideas?

Note: the $eg bit is a comparison helper from a 3rd-party package.

Upvotes: 0

Views: 50

Answers (1)

Matt K
Matt K

Reputation: 4948

Meteor doesn't make you follow a strict MVC, but you're essentially trying to cram a bunch of logic into the view layer. Instead, move all this logic into a single helper.

{{#if isEqual box_group_id ../_id._str}}

Template.foo.helpers({
  isEqual: function (id1, id2) {
    return idStr(id1) === idStr(id2);
  }
});
function idStr(id) {
  return id && id._str || id;
}

Now when you wake up a week from now, you'll be able to read your html & understand what's going on.

Upvotes: 1

Related Questions