Search code examples
dynamiccoldfusionevaluate

Execute coldfusion code stored in a string dynamically?


I have an email body stored as a string in a database, something like this:

This is an email body containing lots of different variables. Dear #name#, <br/> Please contact #representativeName# for further details.

I pull this field from the database using a stored proc, and then I want to evaluate it on the coldfusion side, so that instead of "#name#", it will insert the value of the name variable.

I've tried using evaluate, but that only seems to work if there's just a variable name. It throws an error because of the other text.

(I can't just use placeholders and a find/replace like this - Resolving variables inside a Coldfusion string, because the whole point of storing this in a database is that the variables used to build the string are dynamic. For example, in one case the name field can be called "name" and in another it could be "firstName", etc.)


Solution

  • I would loop over each #variableName# reference and replace it with the evaluated version.

    A regex will be able to find them all and then a loop to go over them all and just evaluate them one by one.