views:

827

answers:

4

I was horsing around and figured it would be nice to move my application variables into a table that can be loaded onApplicationStart.

My intent is to allow ANT to roll out the app, and change a few settings in the database and the proverbial presto..

In my test code, the application.cfc has a simple query to call all the variable names and then a cfloop to set each variable within the application scope as application.varname.

There is no error reported onApplicationStart .. however trying to reference the variables gives an undefined type error.

My spider senses tell me this is something small and obvious... any ideas?

Thanks!!

Update 1 : It seems what I'm looking at is setting dynamic variable names and the fact that they are application variables don't seem to have much impact..

http://www.bennadel.com/blog/152-Dynamic-ColdFusion-Variables-Via-Quoted-Naming.htm

+2  A: 

use cfloop + cfset not cfoutput

Chris Nava
I'll try out a cfloop. I think the main problem might have been that I need to define variables by the quoted naming as per the link I added above..
Jas Panesar
+1  A: 

If I'm reading your question correctly, you're saying that you're setting the application variables inside of a cfoutput tag?

Are you using cfoutput like

<cfoutput query="queryName">
  <!--- Setting code in here --->
</cfoutput>

You should use cfloop rather than cfoutput

<cfloop query="queryName">
  <cfset application.varName = queryName.varName />
</cfloop>

It's kind of hard to help without some code though.

My question is: why are you storing your application variables in a database to begin with if you're just going to dump them back into the application scope?

Hooray Im Helping
So I can set them in a database (Paths, application name, etc). I don't like going into my code to change settings. If you look at the link I added above, it was a matter of addressing the dynamic variable name with Double quotes. I had written cfoutput but I meant cfloop as it's what I was using.
Jas Panesar
+2  A: 

The answer to my question was to set dynamic variable names by quoting the names..

<!--- Loop over the girls and alter the values. --->
<cfloop index="intGirl" from="1" to="3">

<!--- Randomly pick 1 (true) or 0 (false). --->
<cfif RandRange( 0, 1 )>

<!--- Set the dynamic variable naming used quoted evaluation. --->
<cfset "Girl#intGirl#" = "super sexy" />

</cfif>

</cfloop>

More here...

http://www.bennadel.com/blog/152-Dynamic-ColdFusion-Variables-Via-Quoted-Naming.htm

Jas Panesar
+5  A: 

I do not know if the author was advocating that syntax or merely demonstrating that it works, as a point of interest.

Personally, I prefer array notation. I think it helps promote good scoping habits.

<!--- array notation --->
<cfset scope["staticName"& dynamicPortion] = "some value"> 

<!--- example 1  --->
<cfset variables["baseName"& x] = "oh brother">
<!--- example 2  --->
<cfset variables["baseName#x#"] = "oh brother">
I agree -- when someone comes behind to maintain your code, finding <cfset "foo" = "bar" /> is going to be jarring, if not confusing. +1 for Array notation.
Adam Tuttle