I have come across a scenario that I don't know how to avoid using Evaluate. I have stored in a database table session vars and when I output the query results, I want to show the value of the session var.
so here's the set up:
INSERT THE DATA...
<cfquery datasource="TEST" >
insert into testTable (labelVar) values ("session.test") <- I don't want the value of the session var stored I just want to store the session var
</cfquery>
OUTPUT THE RESULTS...
<cfquery name="qExample" datasource="TEST" >
select labelVar from testTable
</cfquery>
<cfset session.test="Hi!">
<cfset TextValue = Evaluate("#qExample.LabelVar#")>
<cfoutput>#TextValue#</cfoutput> <-Outputs Hi!
Now this all works BUT how do I accomplish this without using Evaluate?
You can use structGet()
here, if the value in the DB purely is a scoped variable name:
<cfset TextValue = structGet(qExample.LabelVar)>
I did some research into this allegation that evaluate()
is intrinsically bad. I think it's often avoidable, but it doesn't really seem to be much of a problem, these days: '"evalulate() is really slow". Is it now?'
It is, however, vitally important when considering using evaluate()
to be mindful of Leigh's comment below:
One thing to keep in mind is how it is used. Evaluate is not picky about what it executes. If used on client supplied values, FORM, URL, it can pose some risks, so use it sparingly and wisely IMO.