Handy Coldfusion tips and tricks
I've got some free time to kill so I'd like to take a few moments to pass on little tricks or things about Coldfusion that I've learned along the way.
Working With LIsts
as opposed to
<cfset myList = "one,two,three">
<cfset myList = ListAppend(myList,"somevalue")>
<cfset myList = myList & "somevalue,">
Taking full advantage of List delimiters
As you might know, Coldfusion can turn ANY string into a list, simply by defining a delimiter. The comma is the standard delimiter and one that most people use. However, you might now know that you can use the pipe character "|" as a delimiter along with the comma to build a flexible string that allows you to use Coldfusion's powerful List functions. Here's some code:
What if you wanted to get a count of all of the words in a string. Some people might do some sort of loop over the string and count the number of spaces but that's a lot of code. Why not do it in one line like so:
<cfset couples = "andy,jaime|matt,aimee|shawn,leslie">
<cfloop index="couple" list="#couples#" delimiters="|">
<cfloop index="person" list="#couple#" delimiters=",">
Handy Code Shortcuts
<cfset gettysburg = "Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.">
<cfset totalwords = ListLen(gettysburg," ")>
When performing a CFIF statement when working with values that will be either true/false, yes/no, 1/0 etc. you can save a tiny snippet of code each time by stating this:
instead of this
<cfset amITrue = "yes">
This one's not directly Coldfusion related but it does have to do with coding. If you're like me and handcode everything then finding yourself a good text editor is essential. Just as important is finding a text editor that offers code completion. My text editor, EditPlus (EP), offers user defined code snippets. Type a word and EP completes the rest of the code for you AND in some cases places the cursor where you'd need to start typing next. Here's an example.
<cfset amITrue = "yes">
<cfif amITrue IS "yes">
I type "<cfquery" and then a space and EP gives me this (and places my cursor between the two CFQUERY tags.
I type "INSERT" and a space and EP gives me this:
<cfquery name="" dataSource="#Application.DSN#" dbType="#Application.DBType#" username="#Application.username#" password="#Application.password#">
So you can see that not only does having a text editor with customizable auto complete save time, but it also decreases errors because I'm not having to type those pieces of code each time.
INSERT INTO table_name (
That's it for right now, more to come later. Feel free to add your own time savers to this post but please make sure to test your code and document how it saves time.
Now it's time for "Fun with Structures".
Structures are my second favorite data type, second only to lists that is. Structures are similar to arrays, but allow you to "name" the indexes, which makes them a perfect choice a wide range of things. Here's some Structure basics before we get into some of the cooler things.
There are several ways to create a structure.
<cfset myStructure = StructNew()>
The above line not only creates a structure named "anotherStructure", but also creates a variable inside that structure named "myVar" and assigns it the value "creole rocks".
<cfset anotherStructure.myVar = "creole rocks">
You can also create a structure from within a cfscript tag:
Structures can contain any of Coldfusion's variables types:
aStruct = StructNew();
The preceding code creates a structure named "structOne" with a 3 index array contained within it. The array could have just as easily been a list, or another structure or even a function assignment, Coldfusion gives you that much power.
<cfset structOne = StructNew()>
<cfset structOne.myArray = ArrayNew(1)>
<cfset structOne.myArray = "jaime">
<cfset structOne.myFunction = CreateObject("component", "temp")>
Structures are great for containing values that need to be named. Unlike arrays (which have simple numbered elements), structures allow you to name it's members. How about we create a colors structure? This will contains a "lookup table" of sorts for color names to their corresponding hex values:
Let's examine what we did above. The first line creates the structure (we could have just as easily left out this line). The second line creates a variable named "red" in the "myColors" structure and assigns it a value. Now, what's going on here in the third line? We're using bracket notation...that's right, I haven't shown this one yet. Coldfusion allows you to place a string inside brackets to create a variable. There's also something special to be said about this particular string...it has a space in it, and it will not throw an error. That's because structures (and bracket notation) allow for this.
<cfset myColors = StructNew()>
<cfset myColors.red = "FF0000">
<cfset myColors["navy blue"] = "0000CC">
<cfset tmpColor = "green">
<cfset myColors[tmpColor] = "006600">
In the last two lines I'm doing something even different. I'm creating a string variable named "tmpColor" and assigning it a value of "green". Then I'm creating a dynamic variable inside the myColors structure with the value of "green". Pretty cool eh? I know, I know...what about "real world" application you ask. It's easy to throw out some examples and forget that you want to know how to use structures in your actual code. Well, here you go.
If we continue using the myColors structure, we can colorize an entire page using just the structure assignments we've already made. By calling the color where we want it to appear, the structure will reference the named variable and output it's value,
So let's all cheer for structures shall we?
<div style="background:###myColors["green"]#">This DIV will have a green background.</div>
<div style="background:###myColors["red"]#">This DIV will have a red background.</div>
<div style="background:###myColors["navy blue"]#">This DIV will have a blue background.</div>
<span style="color:###myColors["red"]#">This text will be red</span>, <span style="color:###myColors["green"]#">This text green</span>, etc.
CF Tags in CFSCRIPT blocks
just found the following article on using CF tags in CFSCRIPT block - very interesting read!