CFSCRIPT ColdFusion Cheat Sheet

FOR Loop
for (i=1;i LTE ArrayLen(array);i=i+1) {
	WriteOutput(array[i]);
}
User Defined Function (UDF)
function funky(mojo) {
	var dojo = 0;
	if (arguments.mojo EQ dojo) {
		return "mojo";
	}
	else { return "no-mojo"; }
}
Switch Case
switch(fruit) {
    case "apple":
         WriteOutput("I like Apples");
         break;
    case "orange":
         WriteOutput("I like Oranges");
         break;
    default: 
         WriteOutput("I like fruit");
}
FOR IN Loop
struct = StructNew();
struct.one = "1";
struct.two = "2";
for (key in struct) {
	WriteOutput(key);
}
//OUTPUTS onetwo
If / Else If / Else
if (fruit IS "apple") {
	WriteOutput("I like apples");
}
else if (fruit IS "orange") {
	WriteOutput("I like oranges");
}
else {
	WriteOutput("I like fruit");
}
Try / Catch / Throw / Finally / Rethrow
try {
	//throw CF9+
	throw(message="Oops", detail="xyz");
} catch (any e) {
	WriteOutput("Error: " & e.message);
	rethrow; //CF9+
} finally { 
	//CF9+
	WriteOutput("I run even if no error");
}
Transactions CF9+
transaction {
	//do stuff
	if (good) {
		transaction action="commit";
	else {
		transaction action="rollback";
	}
}
Single Line Comments
mojo = 1; //THIS IS A COMMENT
Multi-Line Comments
/* This is a comment
	that can span
	multiple lines
*/
While Loop
x = 0;
while (x LT 5) {
	x = x + 1;
	WriteOutput(x);
}
//OUTPUTS 12345
Do / While Loop
x = 0;
do {
 x = x+1;
 WriteOutput(x);
} while (x LTE 0);
// OUTPUTS 1
Set A Variable
foo = "bar";
CFSCRIPT Block
<cfscript>
if (foundeo IS "consulting") {
  //...
}
</cfscript>
CFInclude CF9+
include "template.cfm";
CFScript Component CF9+
component extends="Fruit" output="false" {
	
	property name="variety" type="string";
	
	public boolean function isGood() { 
		return true;
	}
	
	private void eat(required numeric bites) {
		//do stuff
	}
}


Tip: Don't forget to put a semicolon; at the end of each statement.

Questions, comments, criticism, or requests can be directed Here

Copyright © 2007-2012 Peter Freitag (http://www.petefreitag.com/), All Rights Reserved. This document may be printed freely as long as this notice stays intact.