Search code examples
javascriptnode.jshandlebars.jsexpress-handlebars

How to insert an handlebarsjs file inside an other


I want to have something like this:

<body>

{{> header}}

</body>

Where header is referencing to header.hbs (main.hbs is at the same directory)

I'm creating the partial with:

exphbs.create("header", "{{header}}")

but The partial header could not be found


Solution

  • Reusable hbs templates are called "partials". You can write these in a separate directory to your page (or "views") templates.

    Tell Express where these are stored in your app entry point file - usually app.js or index.js with

    Handlebars.registerPartials('../path/to/partials/directory')

    Then you can use any of your partials templates in your views with below syntax

    {{>partialName}}

    See docs for further info:

    https://handlebarsjs.com/guide/partials.html#basic-partials