在下划线模板中,我可以执行以下逻辑:

<% if(type === 'video') { %>
    // Something
<% } %>


我可以在车把上做类似的事情吗?尝试了一下,但是不起作用:

{{#if type === 'video'}}
    // Something
{{/if}}


也尝试过使用助手,但仍然没有运气:

Handlebars.registerHelper('isVideo', function(type) {
    if(type === 'video') {
        return true;
    }
    return false;
});

{{#if isVideo type}}
    // Something
{{/if}}

最佳答案

有一种方法

 Handlebars.registerHelper('ifCond', function(v1, v2, options) {
  if(v1 === v2) {
    return options.fn(this);
  }
  return options.inverse(this);
});


然后,您可以像这样在模板中调用帮助程序

 {{#ifCond v1 v2}}
    {{v1}} is equal to {{v2}}
{{else}}
    {{v1}} is not equal to {{v2}}
{{/ifCond}}

关于javascript - 如何在HandleBars模板中执行IF逻辑?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18965083/

10-11 09:19
查看更多