2017-02-27 43 views
0

嗨我想检查HB中的字符串响应。我试过这个:检查句柄状态

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

我该如何检查回复简单?如果它是真的,我想显示一条消息,如果它是错误的,则显示另一条消息。

+0

可能回答在http://stackoverflow.com/questions/17499742/how-do-i-add-console-log-javascript-logic-inside-of-a-handlebars-template请检查。 –

回答

0

把手设计非常简单,并且没有开箱即用的功能。你应该通过状态为一个布尔而不是一个字符串,那么就使用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}} 
+0

我有这个错误:“”缺少帮手:“ifEq \”“, ”name“:”错误“, ”stack“:”错误:缺少帮手: – Hateres1990

+0

您需要注册帮助功能如上'Handlebars.registerHelper' – Bill

+0

我刚刚复制了您的确切代码。 – Hateres1990