handlebars.js {{#if}}条件中的逻辑运算符 [英] Logical operator in a handlebars.js {{#if}} conditional

查看:1393
本文介绍了handlebars.js {{#if}}条件中的逻辑运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在把手JS中有没有将逻辑运算符合并到标准handlebars.js条件运算符中的方法?像这样:

  {{#if section1 ||第2节}} 
..内容
{{/ if}}

I知道我可以写自己的帮手,但首先我想确保我没有重新发明轮子。

这可以通过与帮助者作弊来实现。这可能违背了开发Handlebars的人的意识形态。

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

然后您可以像这样在模板中调用助手

  {{#ifCond v1 v2}} 
{{v1}}等于{{v2}}
{{else}}
{{v1}}不等于{{v2}}
{{/ ifCond}}


Is there a way in handlebars JS to incorporate logical operators into the standard handlebars.js conditional operator? Something like this:

{{#if section1 || section2}}
.. content
{{/if}}

I know I could write my own helper, but first I'd like to make sure I'm not reinventing the wheel.

解决方案

This is possible by 'cheating' with a block helper. This probably goes against the Ideology of the people who developed Handlebars.

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

You can then call the helper in the template like this

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

这篇关于handlebars.js {{#if}}条件中的逻辑运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆