不能将表达式放入另一个表达式中:
{{> checkbox id="test" labelText="test" isChecked="{{someCondition}}" }}
从您编写的示例中,我假设您遇到的问题与您如何传递上下文有关-
id
和
labelText
是硬编码的,而
isChecked
应为某种类型的变量。实际上,所有这些都应该是变量。请考虑以下示例-html:
<div id="content"></div>
<script id="parent-template" type="text/x-handlebars-template">
{{#each checkboxes}}
{{> checkbox this }}<br>
{{/each}}
</script>
<script id="partial-template" type="text/x-handlebars-template">
<input
type="checkbox"
id="{{id}}"
name="{{id}}"
value="true"
{{#if isChecked}}checked{{/if}}>
{{labelText}}
</script>
JS:
var parentTemplate = Handlebars.compile($("#parent-template").html());
Handlebars.registerPartial({
checkbox: Handlebars.compile($("#partial-template").html())
});
$('#content').html(parentTemplate(
{checkboxes: [
{id: 1, labelText: "test 1", isChecked: true},
{id: 2, labelText: "test 2", isChecked: false},
]}
));