2021-01-18 18:40:19 +01:00
|
|
|
class Helper {
|
|
|
|
constructor(name, fn) {
|
|
|
|
this.name = name
|
|
|
|
this.fn = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
register(handlebars) {
|
|
|
|
// wrap the function so that no helper can cause handlebars to break
|
2021-10-11 15:53:55 +02:00
|
|
|
handlebars.registerHelper(this.name, (value, info) => {
|
|
|
|
const context = info?.data?.root
|
|
|
|
return this.fn(value, context || {}) || value
|
2021-01-18 18:40:19 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
unregister(handlebars) {
|
|
|
|
handlebars.unregisterHelper(this.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Helper
|