Best practice for merging variables into a string template

Welcome to the Make community!

Yes, that is possible. I highly recommend using the free CustomJS integration for this to save on operations.

Screenshot_2024-08-09_220841

You’ll need a minimum of one “Execute Inline JavaScript Code” module:

Example Input/Output

Screenshot_20240822_155055

Module Export - quick import into your scenario

You can copy and paste this module export into your scenario. This will import the modules (with fields/settings/filters) shown in my screenshots above.

  1. Move your mouse over the line of code below. Copy the JSON by clicking the copy button on the right of the code, which looks like this:

  2. Enter your scenario editor. Press ESC to close any dialogs. Press CTRLV (paste keyboard shortcut for Windows) to paste directly in the editor.

  3. Click on each imported module and re-save it for validation. There may be some errors prompting you to remap some variables and connections.

JSON module export — paste this directly in your scenario

{"subflows":[{"flow":[{"id":228,"module":"util:SetVariables","version":1,"parameters":{},"mapper":{"variables":[{"name":"template","value":"You are a marketing consultant working for {company-name} and are tasked with {assignment}."},{"name":"replacements","value":"{ \n  \"company-name\": \"Your Company Name\",\n  \"assignment\": \"Your Assignment\"\n}"}],"scope":"roundtrip"},"metadata":{"designer":{"x":73,"y":-2120}}},{"id":223,"module":"custom-js:inlineexecutev2","version":1,"parameters":{"__IMTCONN__":2457341,"jscode":"return input.string.replace(/{([^{}]*)}/g, (match, key) => input.replacements[key] || match);","returnValueType":"text"},"mapper":{"input":"{\n  \"string\": \"{{228.template}}\",\n  \"replacements\": {{228.replacements}}\n}"},"metadata":{"designer":{"x":318,"y":-2119,"name":"Replace All"},"parameters":[{"name":"__IMTCONN__","type":"account:custom-js2","label":"Connection","required":true},{"name":"jscode","type":"text","label":"JavaScript Code","required":true},{"name":"returnValueType","type":"select","label":"Return Type","required":true,"validate":{"enum":["text","collection","array","binary"]}}]}}]}],"metadata":{"version":1}}

Note: Did you know you can reduce the size of blueprints and module export code like the above, using the Make Blueprint Scrubber?

Hope this helps! Let me know if there are any further questions or issues.

@samliew


P.S.: Did you know, the concepts of about 70% of questions asked on this forum are already covered in the Make Academy. Investing some effort into it will save you lots of time and frustration using Make later!