UNPKG

884 BJavaScriptView Raw
1const Handlebars = require('handlebars')
2
3let options = {}
4
5Handlebars.registerHelper({
6 equal: (a, b, opts) => a === b ? opts.fn(this) : opts.inverse(this),
7 unequal: (a, b, opts) => a !== b ? opts.fn(this) : opts.inverse(this)
8})
9
10/**
11 * Set options
12 */
13exports.setOptions = opts => { options = opts }
14
15/**
16 * Register helper
17 */
18exports.registerHelper = Handlebars.registerHelper.bind(Handlebars)
19
20/**
21 * Render a mustache string as handlebars template
22 * @param {String} template Mustache string
23 * @param {Object} data Template data
24 * @return {String} Render result
25 */
26exports.render = (template, data) => {
27 // ignore files that do not have mustaches
28 if (!/{{([^{}]+)}}/g.test(template)) return template
29
30 const handlebars = Handlebars.compile(template, {
31 strict: options.debug,
32 noEscape: true
33 })
34
35 return handlebars(data, { data: options })
36}