我有一个场景,在这个场景中,我得到了一个布尔数据类型的{{isdone}}
值。
我想要将false
值打印为“pending”,将true
值打印为“Done”。
我正在使用以下代码,但它没有起作用。
{{isdone}} == false ? "pending" : "Done"
^
块表示否则。您现在可以使用^
块来表示else
或false
条件。类似这样的语句应该可以工作:
{{#isdone}}Done{{/isdone}}{{^isdone}}pending{{/isdone}}
{{#isdone}}
Done
{{/isdone}}
{{^isdone}}
pending
{{/isdone}}
ternary
的辅助函数。Handlebars.registerHelper("ternary", function (condition, trueValue, falseValue, options) {
return condition ? trueValue : falseValue;
});
然后在模板中像这样使用它
{{ternary isdone "Done" "pending"}}
if/else
语句(有关更多信息,请参见此问题:https://dev59.com/gm025IYBdhLWcg3wZlLd)。我非常怀疑三元运算符在模板中可能是不可能的。 - Seblor