经过大量努力,我已将handlebarsjs实施到测试网站,该网站会在用户输入数据时显示这些数据。
要以不同的模板样式显示用户输入,我必须以相同的形式多次重复相同的handlebarsjs代码。
有没有办法将重复的把手代码放在外部页面中,然后包含此外部页面来代替重复的把手代码?
我不确定语法,或者即使handlebarsjs可以使用这种方法(我已经尝试了几件事 - 但是无法实现这一点)。
以下是我所剔除的重复代码:
{{# if address_style_one_line }}
{{! address is to be displayed across one line - replaced line breaks with line space }}
{{# if address_style_01 }}
....
{{else}}
{{# if address_style_02 }}
....
{{else}}
{{! address is to be displayed across more than one line - use this format as the template for the address scross one line style }}
{{# if address_style_01 }}
....
{{else}}
{{# if address_style_02 }}
....
{{/if}}
答案 0 :(得分:1)
你应该可以通过局部来完成这个任务。部分允许您在其他模板中重用模板。
<强>文档强>
粗略的例子:
<script id="address-template" type="text/x-handlebars-template">
{{# if address_style_one_line }}
{{> address}}
{{else}}
{{> address}}
{{/if}}
</script>
<script id="address-partial" type="text/x-handlebars-template">
// repeated template goes here
{{! address is to be displayed across one line - replaced line breaks with line space }}
{{# if address_style_01 }}
....
{{else}}
{{# if address_style_02 }}
....
</script>
<script type="text/javascript">
$(document).ready(function() {
// register partial
Handlebars.registerPartial("address", $("#address-partial").html());
});
</script>