If statement 车把条件句

If statement 车把条件句,if-statement,handlebars.js,If Statement,Handlebars.js,当我读到Handlebar时,我感到很惊讶,它不允许你使用一个带有小逻辑的条件,比如(If something===1…),所以我真的被困在了如何使它成为Handlebar模板上 if {{price_type}} == 1 Example else if {{price_type}} == 2 {{min_price}} else No price 车把是如何处理的?谢谢各位,如果helper仅适用于布尔值,则手柄将可用。因此,当您想要对它们执行条件操作时,您需要

当我读到Handlebar时,我感到很惊讶,它不允许你使用一个带有小逻辑的条件,比如(If something===1…),所以我真的被困在了如何使它成为Handlebar模板上

 if {{price_type}} == 1
    Example
 else if {{price_type}} == 2
   {{min_price}}
 else
    No price

车把是如何处理的?谢谢各位,如果helper仅适用于布尔值,则手柄将可用。因此,当您想要对它们执行条件操作时,您需要创建自己的助手

{{#ifPriceType price_type min_type}}{{/ifPriceType}}


Handlebars.registerHelper("ifPriceType",function(price_type,min_type){
if(price_type==1){ return "Example";}
else if(price_type==2){ return min_type;}
else return "No price";
});

您甚至可以在模板本身中实现以下功能:

 if {{price_type}} == 1
    Example
 else 
          if {{price_type}} == 2
          {{min_price}}

          else
          No price

您可以通过编写自己的模板助手来实现这一点

Handlebars.registerHelper('conditionalHelper', function(lValue, rValue, options) {
    if (lValue == rValue) {
        return options.fn(this);
    }
    return options.inverse(this);
});
此帮助程序将接受两个值“lValue”和“rValue”,并根据这些值是否相等返回true或false。我们可以在上面给出的示例中使用这个助手,如下所示-

{{#conditionalHelper price_type 1}}
    Example
{{else}}
    {{#conditionalHelper price_type 2}}
        {{min_price}}
    {{else}}
        No price
    {{/conditionalHelper}}
{{/conditionalHelper}}