【问题标题】:Check status in handlebars检查车把中的状态
【发布时间】:2017-07-17 21:55:14
【问题描述】:

您好,我想检查 HB 中的字符串响应。我试过这个:

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

如何简单地检查响应?如果它是真的,我想显示一条消息,如果它是假的,我想显示另一条消息。

【问题讨论】:

标签: javascript sails.js handlebars.js


【解决方案1】:

Handlebars 的设计非常简单,没有开箱即用的功能。您应该将状态作为布尔值而不是字符串传递,然后只需使用 if 语句:

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

你也可以写一个辅助函数:

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

那么你的车把就变成了:

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

【讨论】:

  • 我收到了这个错误:message": "Missing helper: \"ifEq\"", "name": "Error", "stack": "Error: Missing helper:
  • 你需要用Handlebars.registerHelper注册如上的辅助函数
  • @Hateres1990 请查看车把助手以及如何注册。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 2013-07-29
相关资源
最近更新 更多