简体   繁体   中英

handlebars include partial in index

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM