Meteor Helper Check equality

☆樱花仙子☆ 提交于 2019-12-06 15:12:25
Guillaume

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.

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