1

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?

  • How many rows are you expecting from testTable? – James A Mohler Aug 12 '14 at 23:20
  • 5
    If you want to avoid using evaluate, write your query results to a .cfm file and then cfinclude that file. Personally I think evaluate would be the lesser of two evils here. – Dan Bracuk Aug 13 '14 at 00:40
  • 3
    `evaluate()` has gotten a bad rap over the years (deservedly so), but in the last few years, performance has vastly improved. The only other option is what Dan mentioned about writing to a .cfm and including it, but the performance there will be much worse than `evaluate()`. This is one of the few use cases I have seen where `evaluate()` really is the best option. – Scott Stroz Aug 13 '14 at 01:04

2 Answers2

2

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.

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
  • Perfect Adam! Thank you so much for both the read and option. structGet does work and the knowledge that eval won't drastically hurt performance is a relief. – Rachael Malberg Aug 13 '14 at 21:55
  • 1
    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. – Leigh Aug 14 '14 at 15:04
  • 1
    Good thinking, @Leigh. I've added your comment as a post script to my answer, lest ppl don't read down the comments. – Adam Cameron Aug 14 '14 at 16:10
  • Evaluate() real power comes when using it with DE(): You need to escape hashes inside DE() by adding an extra hash to each one, for this to work. – Charles Robertson Oct 18 '15 at 20:14
0

Adam Cameron's option is best, but here's just a few different ways to achieve your goal.

(I create a zession scope just as a quick test so you can test your options.

<cfset zession = StructNew()>
<cfset zession.test = "Hi there">
<cfset labelvar = "zession.test">

Option 1:

<cfoutput>#evaluate(labelvar)#</cfoutput>

Option 2:

<cfoutput>#zession[listGetAt(labelvar,2,".")]#</cfoutput>

J only bring up Option 2 to point out further options. Is your variable always going to be in the session scope? If so, you can save the key name rather than the whole variable name. So if labelvar was "test" then

<cfoutput>#zession[labelVar]#</cfoutput>

Years ago, Evaluate() was terrible, and I think the lingering stigma is good because it forces developers not to resort to lazy methods when they're avoidable. Above were some ways to avoid the function when it was slower.

Regular Jo
  • 5,190
  • 3
  • 25
  • 47