Handlebar.js to include another template in parent template

老子叫甜甜 提交于 2020-01-10 19:44:47

问题


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 .

Thanks.


回答1:


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>

Then register the partial:

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

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

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

Demo: http://jsfiddle.net/ambiguous/P2BK7/



来源:https://stackoverflow.com/questions/19859931/handlebar-js-to-include-another-template-in-parent-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!