且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Handlebar.js在父模板中包含另一个模板

更新时间:2023-02-11 19:20:58

听起来像是在寻找局部声音.部分是要包含在其他几个模板中的模板片段.您将在模板中执行以下操作:

Sounds like you're looking for a partial. A partial is a template fragment that you want to include in several other templates. You'd do something like this in your templates:

<script id="error" type="text/x-handlebars">
    <!-- Error display stuff goes here -->
</script>
<script id="t" type="text/x-handlebars">
    {{> error}}
</script>

然后注册部分:

Handlebars.registerPartial('error', $('#error').html());

完成后,您可以照常使用#t:

Once that's done, you can use #t as normal:

var t = Handlebars.compile($('#t').html());
var h = t({ ... });

演示: http://jsfiddle.net/ambiguous/P2BK7/