Mike Gorski
Mike Gorski

Reputation: 117

How to arrange handlebars partials in express

I have a working app in express. Right now I need to arrange partials (with hbs module). I have a layout set, navigation as well.

Layout.hbs contains elements as follow:

- {{>nav}}
- {{>body}}
- {{>footer}}

I am using {{> }} notation and it works. In footer section lies basic bootstrap scripts.

Question: What is missing, is that when I want to load a page which requires additional scripts at the bottom - I don't know how to chain it. How to add them in not in body section but in footer.

Cheers!

Upvotes: 2

Views: 1176

Answers (1)

Mike Gorski
Mike Gorski

Reputation: 117

Basically the question was how to create dynamic layout(s) with the possibility to add partials and to expand them according to the page you are loading.

Short answer is to use inline blocks:

1.Somewhere in layout file you place:

{{#> scripts-block}}
  {{!-- Custom scripts per page could be added. --}}
{{/scripts-block}}

This block is optional so it won't throw an error in case it is missing.

2.Inside a page that is loaded in render():

{{#> layouts/layout }}
  {{#*inline "scripts-block"}}
    <script src="new-script.js"></script>
  {{/inline}}
{{/layouts/layout }}

Now it will work correctly.

Here is a great article with step by step tutorial on how to build it:

power of handlebars

Upvotes: 1

Related Questions