Javascript Jade模板中的Handlebar条件已检查属性
我想我的把手模板,提供给客户看起来像Javascript Jade模板中的Handlebar条件已检查属性,javascript,html,node.js,handlebars.js,pug,Javascript,Html,Node.js,Handlebars.js,Pug,我想我的把手模板,提供给客户看起来像 <input type='checkbox' checked={{isChecked}}> 编译成 <input type='checkbox' checked> 及 这无法编译,据我所知,我们可以直接在您的jade模板中尝试 <input type='checkbox' {{#if isChecked}}checked{{/if}}> 应该保持相同的格式 我建议您创建一个更通用的助手,以便以后可以轻松重用 Ha
<input type='checkbox' checked={{isChecked}}>
编译成
<input type='checkbox' checked>
及
这无法编译,据我所知,我们可以直接在您的jade模板中尝试
<input type='checkbox' {{#if isChecked}}checked{{/if}}>
应该保持相同的格式
我建议您创建一个更通用的助手,以便以后可以轻松重用
Handlebars.registerHelper("checkedIf", function (condition) {
return (condition) ? "checked" : "";
});
然后,您可以在任何模板中使用它:
<script id="some-template" type="text/x-handlebars-template">
...
<input type="checkbox" {{checkedIf this.someField}} />
...
</script>
...
...
这将被渲染为
<input type="checkbox" checked />
or...
<input type="checkbox" />
或
根据
someField
(映射到模板的对象的字段)的值,如何检查负值,如checked if false?@dvidsilva我很久没有使用把手了。。。这行吗<代码>{{checkedIf!this.someField}?不太可能,我最后不得不写checkedIf和checkedIfNot:O
<input type='checkbox' {{#if isChecked}}checked{{/if}}>
Handlebars.registerHelper("checkedIf", function (condition) {
return (condition) ? "checked" : "";
});
<script id="some-template" type="text/x-handlebars-template">
...
<input type="checkbox" {{checkedIf this.someField}} />
...
</script>
<input type="checkbox" checked />
or...
<input type="checkbox" />