从Jade模板的把手条件检查财产

我希望我的把手模板可以提供给客户端

<input type='checkbox' checked={{isChecked}}> 

要么

 <input type='checkbox' {{#if isChecked}}checked{{/if}}> 

我怎样才能编写一个Jade模板来编译呢? 从他们的文档中,如果分配的值是真实的,但是实际上不包括该值,那么将检查的属性包括在内:

 input(type="checkbox", checked="{{isChecked}}") 

编译

 <input type='checkbox' checked> 

我也试过:

 input(type="checkbox", checked={{isChecked}}) 

 input(type="checkbox", {{#if isChecked}}checked{{/if}}) 

这只是不能编译,我明白

那么直接在你的玉模板中试试吧。

 <input type='checkbox' {{#if isChecked}}checked{{/if}}> 

应该保持相同的格式。

捕获

我会build议创build一个更通用的帮助程序,稍后可以轻松地重用

 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的值(映射到模板的对象的字段)