I have two meteor collections i want to use if condition in template to show value where specific id matche each other see what i want to say
Please Correct my if conditon syntex as this data is returning from two mongocollections functions
<div id="exercise">
{{#each mytemp}}
<ul>
<li id="listA" class="list" data-id3={{exerciseMasterId}}>
{{name}}
{{#each my2}}
{{#if my2.exerciseMasterId==mytemp.exerciseMasterId}}
<ul>
<li>
{{equipmentName}}
</li>
</ul>
{{/if}}
{{/each}}
</li>
</ul>
{{/each}}
</div>
{{#if my2.exerciseMasterId==mytemp.exerciseMasterId}}
should be:
{{#if isSame my2.exerciseMasterId mytemp.exerciseMasterId}}
Template.exercise.helpers({
isSame: function(a, b) {
return a == b;
}
});
Meteor’s templates is based on Handlebars: http://handlebarsjs.com …It’s called Spacebars. Spacebars doesn’t allow you to do basic math or logic in your curly brace expressions. You instead refer to “helper” methods (attached to the same template) and pass any necessary arguments for the helper to return a result. Also note: the value of this
in the helper is the value within the template, eg: if you are iterating through an each statement and then call a method, the value of this
will be the object in each iteration:
<template name="PostTemplate">
{{#each posts}}
<title>{{title}}</div>
{{#if hasComments}}
{{> CommentTemplate comments=comments}}
{{/if}}
{{/each}}
</template>
Template.PostTemplate.helpers({
hasComments: function() {
return this.comments && this.comments.length > 0;
}
});
Just a tip from another beginner @mudassir04 - define that helper in a global scope, it’ll become part of your toolbelt!
i.e.
Template.registerHelper('equals', function (a, b) {
return a === b;
});
(or isSame
, whatever works for you)
There are also some great collections of helpers (that I haven’t used yet, but probably should), i.e. (Edit: The package I linked wasn’t really relevant, but they’re out there if you have a hunt)