From b7ab69fb38bb7124cbd180b810016f5561fca76f Mon Sep 17 00:00:00 2001 From: Frank Date: Fri, 17 Dec 2021 15:49:51 -0500 Subject: [PATCH] Handle multiple variables in one string --- src/helpers/cms-content-helper.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/helpers/cms-content-helper.js b/src/helpers/cms-content-helper.js index 1d901d27e..da08d30ba 100644 --- a/src/helpers/cms-content-helper.js +++ b/src/helpers/cms-content-helper.js @@ -30,23 +30,31 @@ export function fetchCmsContentForPage(fmgPage) { // Function to convert a string, into a matching global state item. function mapStringToState(str) { - // Pull the state string out of our dynamic string from the CMS + // Pull all matches out of the string. const regexExp = new RegExp('{(.*?):(.*?)}', 'g'); const matches = [...str.matchAll(regexExp)]; - const stateString = matches.map(m => m[2]).toString(); - // Convert the state string to a state object - let storeState = store.state; + // Our final string value that will be built from the matches. + let stringBuilder = ''; - for (const s of stateString.split('.')) { - if (storeState[s] != undefined) { - storeState = storeState[s]; - } else { - return false; // If we can't find our state variable, break our loop. Don't break our code. + for (const match of matches) { + + // Reset store state for each match. + let storeState = store.state; + + for (const s of match[2].split('.')) { + if (storeState[s] != undefined) { + storeState = storeState[s]; + } else { + return false; + } } + + // Concatenate the string. + stringBuilder = `${stringBuilder} ${storeState}`; } - return storeState; + return stringBuilder.trimStart(); } // Parent function for processWidgetItemForReplacement. This will loop through the parent