views:

202

answers:

4

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("definition", 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.

A: 

They're all there in the Developing ColdFusion 9 Applications guide. (CF8's here)

How to define the stucture (if that is what I need for a key/value pair list)?

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec09f0b-7fe9.html

How to query?

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-6efa.html

How to [do something] at the application start-up?

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7d39.html#WS6B589054-1E47-45e3-99F0-BAD7FFDF0E92

The best way to do the string replacement

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec22c24-7a49.html

Henry
Thanks, but the guide (and the reference) is sitting next to me in another window. The problem is trying to string together 20 different concepts into some working code when I'm quite literally seeing CFML/CFScript for the first time.
richardtallent
Break the big problem into smaller ones, learn, digest and solve them at your own rate. Good luck. :)
Henry
+3  A: 

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
John Whish
I must be using an older version of ColdFusion... I get `Invalid token '{'` on the line for `cfset application.constants`.
richardtallent
Ah ok, you can find out what version you are using by using SERVER.ColdFusion.ProductVersion. You can't use the shorthand syntax for structs before CF8 but you can create them like:application.constants = StructNew();application.constants.key1 = 123;application.constants.key2 = "ABC";
John Whish
A: 

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.

Ben Doom
Thanks... corrected, but it's failing on the line above, the ArrayLen() call.
richardtallent
Try passing the array of structs as a function argument, instead of attempting to call the application scope from within your function. eg ReplaceDefinitions(inputstring, definedterms) and reference as part of the arguments scope.
Ben Doom
+1  A: 

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>
Leigh