Skip to main content
Tami KB
Inspiring
February 8, 2010
Answered

Evaluate embedded CF variables inside a CF variable

  • February 8, 2010
  • 3 replies
  • 1330 views

I have a variable I would like to display both the contents of AND evaluate the 'value' of the variables inside and am having issues w/ CF8 evaluating both....

<cfset foo = 'This is a test of the #ppl.getemail()# email account. The password is #ppl.getpassword()#'>

where ppl.getemail() is 'Jane@foo.com' and ppl.getpassword() is '123abc'

I would like to cfoutput foo and have the results evaluated as ' This is a test of Jane@foo.com email account. The password is 123abc'
(where the actual value of ppl.getemail() and ppl.getpassword() are evaluated as 'foo'is evaluated)

I have tried cfoutput, cfcontent, evaluate and de, but all print w/o evaluating the the cfc calls. I get literally the variable names in the output instead of their values.

Any suggestions?

This topic has been closed for replies.
Correct answer Adam Cameron.

Thanks Adam, that is exactly what I did, (but I was looking for an elegant CF method <wink>).

Tami


There is nothing elegant about all this evaluate() & DE() stuff that people try to use.  Doing it the replaceList() way is much clearer.

--

Adam

3 replies

BKBK
Community Expert
Community Expert
February 8, 2010

Much simpler:

<cfset foo = "This is a test of the " & ppl.getemail() & " email account. The password is " & ppl.getpassword()>

<cfoutput>#foo#</cfoutput>

ilssac
Inspiring
February 8, 2010

That takes the combonation of evaluate and de() [delay evaluate].

<cfset aString="Here is a string with a ##variable##">

<cfset variable="Something changed">

<cfoutput>#evaluate(de(aString))#</cfoutput>

But once you learn this, I'll be you will quickly see that it is not as usefull as you thought it would be.

Inspiring
February 8, 2010

That's very odd.  What happens when you run this code?

<cffunction name="abc"  returntype="string" access="private">
<cfreturn "abc">
</cffunction>
<cfset x = "abc is #abc()#">
<cfoutput>#x#</cfoutput>

Tami KB
Tami KBAuthor
Inspiring
February 8, 2010

Nope, this didn't work. I need the variable to be evaluated and the variables inside evaluated on the fly (as in a loop).

Dan, the issue may stem from the fact the the embedded variables are set when the user types them into a text string. The user wants to have an editable email message that still allows them to embed CF variables inside the email.

so your code works; until I put it into the editor and then evaluate x... then #x# cfoutputs to "abc is #abc()#"

BKBK, I would love to do it this way, but since this is a user 'editable' email, the entire string gets evaulated, and I get the & signs printed out as well.

Ian, Great idea too... I tried it. But, since the text may contain html (<p>), CF thinks the "<" is a misplaced less than sign and errors "Did you mean LT or LTE?"....

Inspiring
February 8, 2010

Nope, this didn't work. I need the variable to be evaluated and the variables inside evaluated on the fly (as in a loop).

In this case I'd not use a variable, I'd just have place-holder text which I replace-out with the user-entered value @ runtime:

This is a test of the {email} email account. The password is {pwd}

Use replaceList() to replace the placeholders with the actual values.

--

Adam