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) />
source share