Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebar.js to include another template in parent template

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.

like image 861
vinit sharma Avatar asked Nov 08 '13 13:11

vinit sharma


1 Answers

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/

like image 171
mu is too short Avatar answered Sep 24 '22 02:09

mu is too short