express-handlebars-sections
This module add support to express-handlebars to manage sections. It means that you can define one or more sections in the template, and then in the view specify what is the content for each section.
Installation
$ npm install express-handlebars-sections
Usage
1- Configure the express handlebars.
If you register the Handlebars view engine directly use this:
var express = ;var express_handlebars = ;var express_handlebars_sections = ; var app = ; app;
or if you prefer register Handlebars view engine with a Handlebars instance use this:
var express = ;var express_handlebars = ;var express_handlebars_sections = ; var app = ; var hbs = express_handlebars;; // CONFIGURE 'express_handlebars_sections' app;
2- Define your sections in the layout
Here we going to define two sections, one for the header and other for the footer of my page.
Hello express-handlebars-sections <!-- DEFINE A 'header' SECTION --> {{{_sections.header}}} {{{body}} <!-- DEFINE A 'footer' SECTION --> {{{_sections.footer}}}
3- Define the content for your sections in the view
Then in a view that will use the previous layout we can define what is the content that we want to render of each section.
This is my Body Content {{#section 'header'}} This is my Header Content{{/section}} {{#section 'footer'}} This is my Footer Content{{/section}}
This will result the following code:
Hello express-handlebars-sections This is my Header Content This is my Body Content This is my Footer Content
Note
It's not necessary define the content for a section. If no content is specified it does nothing.
Use Cases
Define a default content for the section
We can define a default content for the section in the layout. In this case if the view dosen't define a content for the section, its default content will be rendered.
Hello express-handlebars-sections <!-- DEFINE THE DEFAULT CONTENT FOR THE 'header' SECTION -->{{#section 'header'}} Default Header{{/section}} {{{_sections.header}}} ...
In this case if any content for the 'header' section is defined in the view, it will be render the default content. Is necessary define the default content before define the section. for example the following code is incorrect
{{{_sections.header}}} <!-- DEFINE THE DEFAULT CONTENT FOR THE 'header' SECTION -->{{#section 'header'}} Default Header{{/section}}
Require content for the section
You can force to require a content for a section, throwing an Error if no content is defined.
Hello express-handlebars-sections <!-- DEFINE A REQUIRED 'header' SECTION -->{{{_sections._get 'header'}}}
In this case if no content is defined for the 'header' section (either in view or layout) it will trhow the Error "The section 'header' is required.".