Is there a ternary operator in handlebars.js?

后端 未结 4 1202
说谎
说谎 2021-01-07 19:15

In Handlebars, is there a ternary operator? I don\'t mean if else; I mean like a == true ? \"a\" : \"b\".

4条回答
  •  情书的邮戳
    2021-01-07 19:22

    This below code can be used for ternary or any kind of expression eval.

    Warning: please use this code in scenario where eval can be used safely.

    {{#if (myfunc "(a[0] + 1) % 2 === 0" arg1)}}
    
    {{/if}}
    
    {{#if (myfunc "(a[0] + a[1]) % 2 === 0" arg1 arg2)}}
    
    {{/if}}
    

    handlebar helper function

    myfunc: (exp, ...a) => {
        return eval(exp);
      } 
    

提交回复
热议问题