|
|
|
@ -1,14 +1,20 @@ |
|
|
|
class Helper { |
|
|
|
constructor(name, fn) { |
|
|
|
constructor(name, fn, useValueFallback = true) { |
|
|
|
this.name = name |
|
|
|
this.fn = fn |
|
|
|
this.useValueFallback = useValueFallback |
|
|
|
} |
|
|
|
|
|
|
|
register(handlebars) { |
|
|
|
// wrap the function so that no helper can cause handlebars to break
|
|
|
|
handlebars.registerHelper(this.name, (value, info) => { |
|
|
|
const context = info?.data?.root |
|
|
|
return this.fn(value, context || {}) || value |
|
|
|
const context = info?.data?.root || {} |
|
|
|
const result = this.fn(value, context) |
|
|
|
if (result == null) { |
|
|
|
return this.useValueFallback ? value : null |
|
|
|
} else { |
|
|
|
return result |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
|