有時候我們需要在模版中動態(tài)控制元素的display, 可以這么操作, 代碼如下:
// 模版
<div style="display: {{displayIf conditionA 'block' 'none'}};"></div>
// JS 注冊一個helper
Handlebars.registerHelper(
"displayIf",
function (condition, trueValue, falseValue, options) {
if (condition || foo().length) {
return trueValue;
} else {
return falseValue;
}
}
);