问题
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