How to allow attribute in a template to resolve to a data type value

Just want some advise on best way to do this.

The user can save a letter template. They can use parameters like {CompanyEmail} {CustomerFirstName} etc in the template

Then when we sent the letter it will give the attributes to the letter template before it sends the letter. It needs to dynamically resolve the correct attributes not showing {CompanyEmail} .

I have like 35 diff {Attributes} I want to resolve. The only solution I can think of is a shit ton of find and replaces… Seems there must be a better and more scalable and faster way to do this?

Thoughts. @tylerboodman usually has tips and tricks on this…?

:find&replace would probably be best :grimacing:

Recursive find and replace, maybe?

Can you not think of a better way. Thats like 40+ find and replace’s.

Example?

Another way I could see this work out is extracting template as json with regex, that should solve this long loop.

example or demo or anything?

The goal here is regex extraction from JSON:

I have a similar one set up for open ai function calling: where I’m extracting functional arguments to text from this json format:

[{
“type”: “function_call”,
“id”: “fc_12345xyz”,
“call_id”: “call_12345xyz”,
“name”: “get_weather”,
“arguments”: “{"location":"Paris, France"}”
}]

If you don’t mind we can have a look a this with you on your editor. Can I send you a DM.

Would I do the 50 make changes to X then in the side I set the find an replace for an attribute?

Or what better way to keep it maintainable?

I pictured just a long chain of 50 :find&replace…

But you want it easier to read you could make a custom event that takes in the whole Letter-Record’s Text and have another parameter for what the Find and another parameter for what to Replace it with. then within the custom event return the whole text with the replaced value. then chain together the custom events instead and you can rename the custom event trigger actions

Your best bet is to use/build a plugin or in my case, I just ran a script with Toolbox. Just so happens I had one in my sandbox for testing. You can just turn the script into a plugin just to make it easier.

Demo: Replace All Script with Toolbox
Editor: Replace All Script with Toolbox

// 1. Grab inputs from the properties object
const tpl = properties.param1 || "";
let mapping = {};

try {
  mapping = JSON.parse(properties.param2);
} catch (err) {
  console.error("mappingJSON isn’t valid JSON:", err);
}

// 2. Single regex pass to replace {Keys}
const filled = tpl.replace(/\{(\w+)\}/g, (_, key) => {
  // if mapping has it, return it; otherwise blank
  return Object.prototype.hasOwnProperty.call(mapping, key)
    ? mapping[key]
    : "";
});

// 3. Push it back to Bubble
bubble_fn_replaceAll(filled);

I added instructions in the demo but here’s the script in case you want to read it first. AI assisted of course.

Did you make this just for me or were you working on this!? Crazy timing

No worries, I had one for myself to test a while back. Just had to tidy the mess in the page.