ColdFusion static keys / values ​​list?

I have a database table that is a dictionary of certain terms - key, value. I want to load a dictionary from the application scope from the database and store it there for performance (it doesn't change).

I realize this is probably some kind of "framework", but I'm extremely new to ColdFusion (helping another team).

Then I would like to do some simple string replacement on some strings that are output to the browser, iterate over certain terms, and replace the terms with some HTML to define the terms (hover or link, details to come later doesn't matter).

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("definition", qryDefinedTerms.definition)>
    <cfset ArrayAppend(application.definedterms, myHash)>
</cfloop>
</cffunction>

      

The calling page tries to use it like this:

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 return: "DEFINEDTERMS item is undefined in APP".

Edit: making the OnApplicationStart () call work, apparently Cold Fusion application.cfc doesn't look like ASP.NET web.config, changing it not to reset the application.

+2


a source to share


4 answers


There are many individual questions you have asked, but I want to talk to them! You also didn't say which version of ColdFusion you're using, so I'm going to answer with code that will work in ColdFusion 8 and up.

ColdFusion uses a custom Application.cfc file that you place in your web application route (similar to Global.asax in ASP.Net). It has an in method in the onApplicationStart call that is only executed when the application starts (so not for every request). This is a great place to put any constants. Here's a simple example that sets up a structure (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 data from a database, here's an easy way to do it (there are many 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, 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 many ways to solve your question, however, hopefully you find this a good starting point (although I haven't tested it!).

Good luck and welcome to ColdFusion!

  • John
+4


a source


Also fix the array declaration in Application.cfc. It should be ArrayNew (1) , not Array (1). Then try reinitializing your application variables. One way, cfinvoke:

<cfinvoke component="Application" method="OnApplicationStart" />

      



Once you've done that, and done some of the changes that Ben mentioned. The function should work. Note. You can use shorter operators <= and ++ 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>

      

+1


a source


0


a source


Without looking too deeper, I can see that it is:

application.definedterms(thisdef).term

      

should be as follows:

application.definedterms[thisdef].term

      

In CF (like many languages), parens imply a function call, and square brackets imply an array reference.

0


a source







All Articles