1

Forgive the newbie question here, but I am trying to allow for custom variables in a string that I would pull from a query, and replace those with results of another query. So to simplify it, I would need something like this...

<cfset mystring = "This is my %firstname% and this is my %lastname%.">

Then suppose I had query results of MyQuery,first and MyQuery.last, I would like the most efficient way to replace the %firstname% and %lastname% in mystring with the query results.

Any help/example would be so appreciated!

Thanks.

isapir
  • 21,295
  • 13
  • 115
  • 116
mck
  • 413
  • 1
  • 3
  • 14
  • 1
    Possible duplicate of [Resolving variables inside a Coldfusion string](https://stackoverflow.com/questions/1871249/resolving-variables-inside-a-coldfusion-string) – Twillen Nov 06 '17 at 21:40

3 Answers3

2

Are you looking for replaceNoCase()?

<cfset myNewString = replaceNoCase(myString, '%firstname%', myQuery.first, 'all')>
<cfset myNewString = replaceNoCase(myNewString, '%lastname%', myQuery.last, 'all')>
Jules
  • 1,941
  • 15
  • 18
2

If you are using Lucee then you can take advantage of replace(string, struct) (showing cfscript syntax):

template = "This is my %firstname% and this is my %lastname%.";

replaceMap = {
    "%firstname%" : "John"
   ,"%lastname%"  : "Doe"
}

populated = replace(template, replaceMap);    // use replaceNoCase() for case-insensitivity

All of the replacements take place in the Java code which is much more efficient than doing it in a CFML loop.

Also, replace() is much more efficient than replaceNoCase(), so if the CaSe is known in advance you should use replace() where possible, or consider normalizing the case, e.g. with lcase(), prior to calling replace().

isapir
  • 21,295
  • 13
  • 115
  • 116
1

if you had a query like this:

<cfquery name="myquery" datasource="mydatasource">
  select firstname, lastname from users
</cfquery>

then you would out put the values from that by doing the following:

<cfoutput query="myquery">
   <cfset variables.mystring = "This is my #firstname# and this is my #lastname#.">
</cfoutput>
  • Yes, I understand that... I need to REPLACE the contents of a string where %firstname% and %lastname% are found – mck Nov 07 '17 at 00:42
  • @mck Why? If you're querying to get this information anyway, you already have it inside of ColdFusion. ReplaceNoCase() will work, but it would be much more efficient to just use a ColdFusion variable in #s to output a variable. Unless your string is already fixed and you can't replace the %s with #s. – Shawn Nov 07 '17 at 20:49
  • @shawn - This is a scenario in which we allow people to create templates for emails to go out, sh=o they need to be able to save something like: Dear %firstname%, - And whenever we need to send out an email that uses that template, we need to do a query, pull up the values and their template, and send it out. – mck Nov 08 '17 at 22:43
  • Its also useful for 'shortcode' functionality like in wordpress. Users can enter `%shortcode%` in cms text contents. Then later CFML can replace the shortcode with data. – Jules Nov 20 '17 at 02:40