diff --git a/packages/string-templates/src/helpers/Helper.js b/packages/string-templates/src/helpers/Helper.js index 3d7a9316f3..3dc70ba0dc 100644 --- a/packages/string-templates/src/helpers/Helper.js +++ b/packages/string-templates/src/helpers/Helper.js @@ -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 + } }) } diff --git a/packages/string-templates/src/helpers/index.js b/packages/string-templates/src/helpers/index.js index a8c5ee0752..201ecb83da 100644 --- a/packages/string-templates/src/helpers/index.js +++ b/packages/string-templates/src/helpers/index.js @@ -19,7 +19,7 @@ const HELPERS = [ return new SafeString(JSON.stringify(value)) }), // javascript helper - new Helper(HelperFunctionNames.JS, processJS), + new Helper(HelperFunctionNames.JS, processJS, false), // this help is applied to all statements new Helper(HelperFunctionNames.ALL, value => { if ( diff --git a/packages/string-templates/src/helpers/javascript.js b/packages/string-templates/src/helpers/javascript.js index 2fca180d14..cba3966213 100644 --- a/packages/string-templates/src/helpers/javascript.js +++ b/packages/string-templates/src/helpers/javascript.js @@ -36,11 +36,7 @@ module.exports.processJS = (handlebars, context) => { // Create a sandbox with out context and run the JS vm.createContext(sandboxContext) - const result = vm.runInNewContext(js, sandboxContext) - if (result == null || result === "") { - return " " - } - return result + return vm.runInNewContext(js, sandboxContext) } catch (error) { return "Error while executing JS" }