简体   繁体   English

检查车把的状态

[英]Check status in handlebars

Hi I want to check the string response in HB. 嗨,我想检查HB中的字符串响应。 I've tried this: 我已经试过了:

{{#if status=='false'}}
    {{console.log("hi");}}
{{else}}
   {{console.log("no");}}
{{#endif}}

How can I check the response simple? 如何检查响应简单? I want to show a message if it's true and another message if it's false. 我想显示一条消息,如果它是真实的,则显示另一条消息,如果它是false。

Handlebars is designed to be very simple and doesn't have this functionality out of the box. 车把的设计非常简单,没有开箱即用的功能。 You should pass the status as a boolean rather than a string, then just use an if statement: 您应该将状态作为布尔值而不是字符串传递,然后仅使用if语句:

{{#if status}}
    {{console.log("hi");}}
{{else}}
   {{console.log("no");}}
{{#endif}}

You could also write a helper function: 您还可以编写一个辅助函数:

Handlebars.registerHelper('ifEq', function(a, b, options) {
  if (a == b) return options.fn(this)
  else return options.inverse(this)
});

Then your handlebars becomes: 然后您的车把变成:

{{#ifEq status 'true'}}
  Hello
{{else}}
  No
{{/ifEq}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM