有时候我们需要在模版中动态控制元素的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;
}
}
);