Use handlebars as engine for customize


> Use handlebars as engine for customize
```
npm install customize-engine-handlebars
The following examples demonstrate how to use this module.
The following usage example has a configuration for all possible properties
of the Handlebars-engine:
`js`
const 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:
`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).hb-helpers.js
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 -file are registeredtext2.txt.hbs
with Handlebars and uses the shout-helper from hb-helpers.js to turn a
string into upper-case.
`hbs
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.
`js
const got = require('got')
module.exports = async function(data) {
return {
name: data.name,
city: data.city,
github: got('https://api.github.com/users/nknapp').json()
}
}
`
The result is injected into the data as github property and rendered byfooter.hbs
the partial.
`hbs`
------
Github-Name: {{{github.name}}}
The output of this example is:
``
{
"handlebars": {
"subdir/text3.txt": "------\nBlog: https://blog.knappi.org\n",
"text1.txt": "I'm nknapp\n\nI'm living in Darmstadt.\n\n------\nBlog: https://blog.knappi.org\n",
"text2.txt": "I'm nknapp\n\nI'm living in DARMSTADT.\n\n------\nBlog: https://blog.knappi.org\n"
}
}
see docs/examples.md for more examples
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
customize-engine-handlebars` is published under the MIT-license.
See LICENSE.md for details.
See CONTRIBUTING.md.