Variable Resolution Inside Coldfusion String

My client has an email body database table that is sent to clients at specific times. The text for emails contains ColdFusion expressions such as Dear # firstName #, etc. These emails are HTML - they also contain all kinds of HTML markup. What I would like to do is read this text from the database into a string and then process ColdFusion Evaluate () to resolve the variables. When I do this, Evaluate () throws an exception because it doesn’t like HTML markup (I also tried filtering the line through HTMLEditFormat () as an intermediate step for grins, but it didn’t like entities).

My predecessor solved this problem by writing email text to a file and then including it. It works. It seems really hacked. Is there a more elegant way to handle this using something like Evaluate that I don't see?

+4
source share
4 answers

What other languages ​​often do seems to work very well, just have some kind of token in your template that can easily be replaced with a regular expression. So you could have a pattern:

Dear {{name}}, Thanks for trying {{product_name}}. Etc... 

And then you can simply:

 <cfset str = ReplaceNoCase(str, "{{name}}", name, "ALL") /> 

And when you want a more interesting way, you can simply write a method to wrap this:

 <cffunction name="fillInTemplate" access="public" returntype="string" output="false"> <cfargument name="map" type="struct" required="true" /> <cfargument name="template" type="string" required="true" /> <cfset var str = arguments.template /> <cfset var k = "" /> <cfloop list="#StructKeyList(arguments.map)#" index="k"> <cfset str = ReplaceNoCase(str, "{{#k#}}", arguments.map[k], "ALL") /> </cfloop> <cfreturn str /> </cffunction> 

And use it like this:

 <cfset map = { name : "John", product : "SpecialWidget" } /> <cfset filledInTemplate = fillInTemplate(map, someTemplate) /> 
+9
source

Not sure if you need to reinstall, you can overdo it with a simple replacement if you have too many fields to merge

How about something like this (not tested)

 <cfset var BaseTemplate = "... lots of html with embedded tokens"> <cfloop (on whatever)> <cfset LoopTemplate = replace(BaseTemplate, "#firstName#", myvarforFirstName, "All"> <cfset LoopTemplate = replace(LoopTemplate, "#lastName#", myvarforLastName, "All"> <cfset LoopTemplate = replace(LoopTemplate, "#address#", myvarforAddress, "All"> </cfloop> 

Just treat the html block as a simple string.

+9
source

CF 7+: can you use regex, REReplace ()?

CF 9: Use Virtual File System

+2
source

If the variable is in the structure, it’s kind of a form message, then you can use "StructFind". He does the same as you. I ran into this problem while processing a form with dynamic inputs.

Ref.

 StructFind(FORM, 'WhatYouNeed') 
-2
source

All Articles