I wrote a couple helpers that let me use template inheritance, a la Django. Since they're just helpers, they don't need to go into Handlebars proper, but I wanted to share them. Perhaps if they are deemed useful enough, someone can put in the effort to integrate them.
Usage
The first helper is named block. It lets you specify default content.
The second helper is named partial. (I don't know why I chose these names.) It lets you specify overriding content.
In your base template, you declare sections of content that may be replaced by deriving templates. For these sections, you can either use {{#block}}s or normal partial inclusion ({{>}}). The second option serves as a shorthand for when you have no default content.
<!-- base.hbs -->
<title>{{#block "title"}}Untitled{{/block}}</title>
{{> content}}
Then in your deriving templates, you specify any overrides with {{#partial}}s before declaring the inherited template with normal partial inclusion.
<!-- derived.hbs -->
{{#partial "title"}}Title{{/partial}}
{{#partial "content"}}Hello, World!{{/partial}}
{{> base}}
Example output:
<title>Title</title>
Hello, World!
I wrote a couple helpers that let me use template inheritance, a la Django. Since they're just helpers, they don't need to go into Handlebars proper, but I wanted to share them. Perhaps if they are deemed useful enough, someone can put in the effort to integrate them.
Usage
The first helper is named
block. It lets you specify default content.The second helper is named
partial. (I don't know why I chose these names.) It lets you specify overriding content.In your base template, you declare sections of content that may be replaced by deriving templates. For these sections, you can either use
{{#block}}s or normal partial inclusion ({{>}}). The second option serves as a shorthand for when you have no default content.Then in your deriving templates, you specify any overrides with
{{#partial}}s before declaring the inherited template with normal partial inclusion.Example output: