handlebars include partial in index

242 Views Asked by At

I'm just trying to use handlebars in a project. So I have a PartialNavigation.handlebars and index.handlebars, and I just want to include the PartialNavigation to index with one parameter. I was checking the docs since a long time but didn't found what I want. And all I tried failed...

Thanks per advance!

PokeRwOw

1

There are 1 best solutions below

4
richardgirges On BEST ANSWER

In Handlebars, you can register your partial using the Handlebars.registerPartial() method.

Example:

var partialNavData = '<div id="navigation"></div>';

Handlebars.registerPartial( 'partialNavigation', partialNavData );

Registering multiple partials at once:

var partialNavData = '<div id="navigation"></div>';    
var partialFooterData = '<footer>My Footer</footer>';

Handlebars.registerPartial({
    partialNavigation: partialNavData,
    partialFooter: partialFooterData
});

And finally, including your partials would be done like so:

Your index file:

{{>partialNavigation}}

{{>partialFooter}}

** You can use AJAX to grab the PartialNavigation template if it's too large to put into a string.

Example JSFiddle: https://jsfiddle.net/richardgirges/Lkrn1qLL/3/

Read these reference docs for more info