Handlebar.js在父模板中包含另一个模板 [英] Handlebar.js to include another template in parent template

查看:94
本文介绍了Handlebar.js在父模板中包含另一个模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个错误页面,它是一个handlebar.js模板.我需要在当前页面的handlebar.js模板中加载此模板.典型的包括<文件名>类型,但在车把中找不到解决方法.请在这里告诉我.

I have an error page , which is a handlebar.js template. I need to load this template in the current page handlebar.js template. A typical include < file name > type, but could not found a way around in handlebar. Please advise me here .

谢谢.

推荐答案

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

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/

这篇关于Handlebar.js在父模板中包含另一个模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆