【问题标题】:if else in handle bar templateif else 在车把模板中
【发布时间】:2015-09-06 06:54:38
【问题描述】:
In need to check this scenario using handle bar templates
if(data.Id==1 && Isreal==false)
{
//`enter code here
}
else if(data.id==2 && IsReal==true)
//other html
else
`//enter code here`last html

我该怎么做,我尝试使用 helper 但它在我的情况下不起作用

【问题讨论】:

  • Handlebars.js Else If的可能重复
  • 我可以比较模板 html 中的值吗?
  • {{#if Status==1}} ///一些 Html {{#else}} {{#if status==2}} //一些 html {{#if}} { {#if}}这是我需要比较值的场景..这可能吗?请帮助
  • {{#if Status==1}} ... {{else}} {{#if status==2}} ... {{else}} ... {{/if}}{{/if}}

标签: handlebars.js handlebarshelper


【解决方案1】:

最佳实践是不在模板中执行任何业务逻辑,而是在之前执行并通过上下文将其传递到模板中。

var context = {
    dataOneNotReal = data.Id==1 && Isreal==false,
    dataTwoIsReal = data.id==2 && IsReal==true
};

然后在模板中

{{#if dataOneNotReal}}
    // code
{{else}}
    {{#if dataTwoIsReal}}
        // code
    {{else}}
        // code
    {{/if}}
{{/if}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-08
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    相关资源
    最近更新 更多