ColdFusion static key/value list?
I have a database table that is a dictionary of defined terms -- key, value. I want to load the dictionary in the application scope from the database, and keep it there for performance (it doesn't change).
I gather this is probably some sort of "struct," but I'm extremely new to ColdFusion (helping out another team).
Then, I'd like to do some simple string replacement on some strings being output to the browser, looping through the defined terms and replacing the terms with some HTML to define the terms (a hover or a link, details to be worked out later, not important).
This is the code currently in the application.cfc file:
<cffunction name="onApplicationStart">
<cfquery name="qryDefinedTerms" datasource="mydsn">
SELECT term, definition FROM definedterms
</cfquery>
<cfset application.definedterms = Array(1)>
<cfloop query="qryDefinedTerms">
<cfset myHash = structNew()>
<cfset myHash.put("term", qryDefinedTerms.term)>
<cfset myHash.put("definitio开发者_StackOverflown", qryDefinedTerms.definition)>
<cfset ArrayAppend(application.definedterms, myHash)>
</cfloop>
</cffunction>
The calling page attempts to use it as follows:
function ReplaceDefinitions(inputstring) {
for (thisdef = 1 ;
thisdef LTE ArrayLen(application.definedterms);
thisdef = (thisdef+1)) {
inputstring = Replace(inputstring,
application.definedterms(thisdef).term,
application.definedterms(thisdef).definition, "ALL");
}
return inputstring;
}
When I call the function, I get back: "Element DEFINEDTERMS is undefined in APPLICATION".
Edit: forcing a call to OnApplicationStart() worked, apparently Cold Fusion's application.cfc isn't like ASP.NET's web.config, changing it doesn't reset the application.
There are a lot of separate questions you've asked but I'll have a go at answering them! You also haven't said what version of ColdFusion you are using, so I'm going to answer with code that will work in ColdFusion 8 and higher.
ColdFusion uses a special file called Application.cfc that you put in the route of your web application (similar to Global.asax in ASP.Net). It has a method in in called onApplicationStart that is only executed when the application starts (so not on each request). This is a great place to put any constants. Here is a simple example which sets a struct (like a map in other languages) using the {} syntax:
Application.cfc
<cfcomponent> <cffunction name="onApplicationStart"> <!--- set global constants here ---> <cfset application.foo = { a=1, b=2, c="my string" }> </cffunction> </cfcomponent>
If you want to get the data from a database, here is a simple way to do it (there are lots of other ways that are probably better but this should get you started!)
<cfcomponent> <cffunction name="onApplicationStart"> <!--- set global constants here ---> <cfset application.datasource = "mydsn"> <cfquery name="qryConstants" datasource="#application.datasource#"> select key, value from tblConstants </cfquery> <cfset application.constants = {}> <cfloop query="qryConstants"> <cfset application.constants[ qryConstants.key ] = qryConstants.value> </cfloop> </cffunction> </cfcomponent>
As for replacing values in a string then you can do something like this:
somescript.cfm
<cfsavecontent variable="somestring"> Hello, ${key1} how are you? My name is ${key2} </cfsavecontent> <!--- replace the ${key1} and ${key2} tokens ---> <cfloop collection="#application.constants#" item="token"> <cfset somestring = ReplaceNoCase( somestring, "${#token#}", application.constants[ token ], "all" )> </cfloop> <!--- show the string with tokens replaced ---> <cfoutput>#somestring#</cfoutput>
As I said there are lots of ways to solve your question, however hopefully you'll find this a good starting point (although I haven't tested it!).
Good luck and welcome to ColdFusion!
- John
Also, fix your array declaration in the Application.cfc. It should be ArrayNew(1) rather than Array(1). Then try reinitializing the application variables. One way is using cfinvoke:
<cfinvoke component="Application" method="OnApplicationStart" />
Once you have done that, and made some of the changes Ben mentioned. The function should work. Note: You can use the shorter <= and ++ operators if you are using CF8+
<cfscript>
// Added variable scoping
function ReplaceDefinitions(inputstring) {
var thisdef = "";
var newString = arguments.inputstring;
for (thisdef EQ 1 ; thisdef LTE ArrayLen(application.definedterms); thisdef = thisdef+1) {
newString = Replace( newString,
application.definedterms[thisdef].term,
application.definedterms[thisdef].definition,
"ALL" );
}
return newString;
}
</cfscript>
Without looking too much deeper, I see that this:
application.definedterms(thisdef).term
should be this:
application.definedterms[thisdef].term
In CF (like many languages) parens imply a function call, and square brackets imply an array reference.
精彩评论