Best practice for merging variables into a string template

Suppose I have a template stored in a variable that looks like this:

“You are a marketing consultant working for {company-name} and are tasked with {assignment}.”

And suppose also that somewhere in the available input bundles for my automation are the variable values I want plugged in for {company-name} and {assignment}.

What I’d like is for this merged output text with the plugged in values to be available as input for downstream modules.

I see how I can manually construct a series of variable assignments and string operations to achieve this. However, that process seems cumbersome and brittle.

Is there a module and/or best practice I should be using for this? Thanks!

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!

How to set-up a FREE Custom JS connection?

  1. Go to the app’s website and click “Create Account”.

  2. Once logged in, on the right-hand side, click on the “Show” link. Copy the API key.

  3. Click “Create a connection” in the Custom JS module:

  4. Paste API key and click Save:

How to use the Custom JS module?

  1. Map a variable (or a JSON object) into the “Input” field.

  2. Write a function to do something with the input variable. If the input is a JSON, you can reference object properties using dot notation.

  3. Write a final statement to return the call of the function you wrote.

Further Information

If you need help with using Custom JS modules, please create a new thread. This helps keep the forum organized and helps me get to your question faster.

You can also take a look at the official documentation here for more advanced usage:

This is fantastic information - thanks so much! I will try this out later today.