@dadi/web-handlebars

1.0.2 • Public • Published

DADI Web

Handlebars.js engine interface

npm (scoped) coverage Build Status JavaScript Style Guide semantic-release

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}`
})

Package Sidebar

Install

npm i @dadi/web-handlebars

Weekly Downloads

0

Version

1.0.2

License

MIT

Last publish

Collaborators

  • annybs
  • josephdenne
  • jimlambie
  • adamkdean