Skip to content

Latest commit

 

History

History
143 lines (101 loc) · 3.81 KB

README.md

File metadata and controls

143 lines (101 loc) · 3.81 KB

customize-engine-handlebars

NPM version Travis Build Status Coverage Status Greenkeeper badge

Use handlebars as engine for customize

Installation

npm install customize-engine-handlebars

Usage

The following examples demonstrate how to use this module.

Configuration

The following usage example has a configuration for all possible properties of the Handlebars-engine:

var customize = require('customize')
customize()
  .registerEngine('handlebars', require('customize-engine-handlebars'))
  .load(require('./config-module.js'))
  .run()
  .then(console.log)

This example loads its configuration from the module config-module.js:

module.exports = function (customize) {
  return customize.merge({
    handlebars: {
      // Directory containing templates
      templates: 'templates',
      // Directory containing partials
      partials: 'partials',
      // JS-file exporting Handlebars helper-functions
      helpers: 'hb-helpers.js',
      // JS-file exporting a preprocessor function
      preprocessor: 'hb-preprocessor.js',
      // Input data for Handlebars
      data: {
        name: 'nknapp',
        city: 'Darmstadt'
      }
    }
  })
}

A quick note: If your are creating a real configuration-module, you should always use require.resolve or __dirname to determine the correct path to referenced files.

All the templates in the templates directory are called with the provided data (name and city). Each one generates an entry in the result of the engine. The templates call a partial that is inserted below the main content. Helper functions from the hb-helpers.js-file are registered with Handlebars and text2.txt.hbs uses the shout-helper from hb-helpers.js to turn a string into upper-case.

I'm {{name}}

I'm living in {{shout city}}.

{{>footer}}

The example also includes a preprocessor (hb-preprocessor.js) that calls the github API to retrieve information about the user.

const got = require('got').extend({
  json: true
})

module.exports = function (data) {
  return {
    name: data.name,
    city: data.city,
    github: got('https://api.github.com/users/nknapp').then(response => response.body)
  }
}

The result is injected into the data as github property and rendered by the footer.hbs partial.

------
Github-Name: {{{github.name}}}

The output of this example is:

{
  "handlebars": {
    "subdir/text3.txt": "------\nGithub-Name: Nils Knappmeier",
    "text1.txt": "I'm nknapp\n\nI'm living in Darmstadt.\n\n------\nGithub-Name: Nils Knappmeier",
    "text2.txt": "I'm nknapp\n\nI'm living in DARMSTADT.\n\n------\nGithub-Name: Nils Knappmeier"
  }
}

More examples

see docs/examples.md for more examples

API-documentation

This package will always support the latest version of NodeJS and as well as the current LTS version. In the future, it will not be considered a breaking change to drop support of a pre-LTS version of NodeJS.

see docs/api.md

License

customize-engine-handlebars is published under the MIT-license.

See LICENSE.md for details.

Release-Notes

For release notes, see CHANGELOG.md

Contributing guidelines

See CONTRIBUTING.md.