Is there a way to check the value of a string in meteor helper?
Lets say i have this helper
Template.example.helpers({
typeOfVideo:function(videoType){
var videoLink = GS.Gems.Collections.Gems.findOne({_id:this._id}).videoLink;
if(videoLink.match(/youtube\.com/)){
return "youtube";
}else if(videoLink.match(/vimeo\.com/)){
return "vimeo";
}else{
return "undefined"
}
}
})
Now i want to check if the video is equal to Youtube, Vimeo or undefined, i could do by returning true/false, but since there are more i want to know what type of video im dealing with
Already try with
{{#if typeOfVideo=youtube}} <!-- also {{#if typeOfVideo 'youtube'}}
youtube
{{/if}}
{{#if typeOfVideo=vimeo}}
vimeo
{{/if}}
And the templates render all the value (youtube,vimeo), im missing somehting?
As Spacebars (Meteor templating system) is a "logic-less" templating system (like Handlebars on which it is based) you cannot directly check for an equality in a if
statement.
But you can create a global helper with two parameters to check for a string equality or matching string like this:
Template.registerHelper('isEqual', function(string, target) {
if( string.match( new RegExp(target, 'g') ) ) {
return true;
}
return false;
});
And use it in your template like this:
{{#if isEqual videoLink 'youtube'}}
<p>Youtube</p>
{{else}}
<p>Vimeo</p>
{{/if}}
There is no else if
in Spacebars, so if you have many cases to check it may be a hassle.
But you could think of injecting the template/content directly from the global helper when possible.
来源:https://stackoverflow.com/questions/30474799/meteor-helper-check-equality