@dadi/web-handlebars
v1.0.2
Published
A Handlebars.js interface for DADI Web
Downloads
6
Readme
Handlebars.js engine interface
This module allows Handlebars.js templates to be used with DADI Web.
Installation
Add this module as a dependency:
npm install @dadi/web-handlebars --save
Include it in the
engines
array passed to Web:require('@dadi/web')({ engines: [ require('@dadi/web-handlebars') ] })
Configuration
The following configuration parameters can be added to the global Web config file, under engines.handlebars
.
paths
Paths required by Handlebars.
- Format:
Object
- Default:
{ { helpers: 'workspace/utils/helpers' } }
Partials
Partials must be registered with Handlebars before they can be used in a template. This library takes care of the registration for you, simply supply the path to your partials in the configuration option additionalTemplates
.
pages/
|_ partials/
|_ |_ common/
|_ |_ |_ header.hbs
|_ contact-info.hbs
|_ home.hbs
Partials are referenced by their relative path, minus the file extension. After loading the above hierarchy of templates and partials, to include header.hbs
from the page contact-info.hbs
, you would use the following syntax:
{{> 'partials/common/header' }}
See the Handlebars documentation for more information regarding the use of partials.
Helpers
To use helpers supply the path to your helpers in the main Web configuration file:
"engines": {
"handlebars": {
"paths": {
"helpers": "workspace/helpers"
}
}
}
Helpers can be individual JavaScript files within the specifed directory, or all in a single file.
Example:
const handlebars = require('handlebars')
/*
* Returns the full name and price of the supplied product
* Usage: {{ renderProduct product }}
*/
handlebars.registerHelper('renderProduct', product => {
return `helper: ${product.name} - £${product.price}`
})