Wax On adds support to Handlebars for template inheritance with the block
and extends
helpers.
Directly inspired by template Inheritance in Pug and works the same way in Handlebars.
npm install --save wax-on
// app.js
const Handlebars = require("handlebars");
const wax = require("wax-on");
// register Wax On helpers with Handlebars
wax.on(Handlebars);
wax.setLayoutPath("/path/to/layouts");
The extends
helper allows a template to extend a layout or parent template. It can then override certain pre-defined blocks of content.
Note: You can have multiple levels of inheritance, allowing you to create powerful hierarchies of templates.
Handlebars blocks can provide default content if desired, however optional as shown below by block scripts
, block content
, and block foot
.
Now to extend the layout, simply create a new file and use the extends
helper as shown below, giving the path (server only). You may now define one or more blocks that will override the parent block content, note that here the foot
block is not redefined and will output “some footer content”.
It’s also possible to override a block to provide additional blocks, as shown in the following example where the content
block now exposes a sidebar
and primary
block for overriding, or the child template could override the content
block all together.
The block
helper also allows you to prepend or append blocks in addition to the default behavior of replacing blocks. Suppose for example you have default scripts in a head
block that you wish to utilize on every page, you might do this:
The append
and prepend
helpers make this common use case even easier.
In chronological order (most recent first); not in order of priority. Please create an issue if you'd like any of these changes or to recommend other changes.
- add support for running in the browser (client-side)
- investigate possibility of using partial blocks in addition to the extends helper
The Wax On change log can be found in the CHANGELOG.md file within the project.
Wax On is available under the MIT License.