Meteor Helper Check equality

自作多情 提交于 2019-12-08 05:28:52

问题


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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!