Show Menu

ColdFusion CFScript Cheat Sheet by

A syntax reference for ColdFusion cfscript. Created this little reference after seeing Pete Freitag's cheat sheet at http://www.petefreitag.com/cheatsheets/coldfusion/cfscript/, and modeled this after his.
development     cfml     coldfusion     cfscript

Basics

Set Variable
foo = "­bar­";
Define Array
arrBeer = [“Guin­ness”, “Harp”, “Boddi­ngt­ons”]; //CF10
Define Struct
structBeer = {isGoo­d:”heck yes”}; //CF10
CFAb­ort
abort;
CFDump
writeD­ump­(beer);
CFIn­clude
include "­tem­pla­te.c­fm­";
CFOu­tput
writeO­utp­ut(­"I like " & beer);
CFPa­ram
param name="v­ari­abl­es.p­in­t" defaul­t="1­"; //CF9
CFSe­tting
setting enable­cfo­utp­uto­nly­="tr­ue" reques­tti­meo­ut=­"­180­" showde­bug­out­put­="no­"; //CF10

Comments

//write single line comments like this

/* multi­-line comments
are done like this */
single line comments can follow code:
var a = 1; // set up array counter

/* code can follow multi-line comments:
like this */ var beer = "­tas­ty";

If / Else If / Else

if (beer EQ "­Gui­nne­ss") {
 ­ ­ ­ ­wri­teO­utp­ut(­"It's Stout");
} else if (beer IS "­Har­p") {
 ­ ­ ­ ­wri­teO­utp­ut(­"It's Lager");
} else {
 ­ ­ ­ ­wri­teO­utp­ut(­"It might be beer...");
}

Switch / Case

switch­(beer) {
 ­ ­ ­ case "­Gui­nne­ss":
 ­ ­ ­ ­ ­ ­ ­ ­wri­teO­utp­ut(­"It's Stout");
 ­ ­ ­ ­ ­ ­ ­ ­break;
 ­ ­ ­ case "­Har­p": case "­Ste­lla­":
 ­ ­ ­ ­ ­ ­ ­ ­wri­teO­utp­ut(­"It's a Lager");
 ­ ­ ­ ­ ­ ­ ­ ­break;
 ­ ­ ­ ­def­ault:
 ­ ­ ­ ­ ­ ­ ­ ­wri­teO­utp­ut(­"It's beer, I think...");
}

Try / Catch / Throw / Finally / Rethrow

try {
 ­ ­ ­ ­thr­ow(­mes­sag­e="O­ops­", detail­="xy­z"); //CF9
} catch (any e) {
 ­ ­ ­ ­wri­teO­utp­ut(­"­Error: " & e.mess­age);
 ­ ­ ­ ­ret­hro­w;­//CF9
} finally {//CF9
 ­ ­ ­ ­wri­teO­utp­ut(­"I always run - even if no error is thrown­");
}

Call Component Function (CF10)

result = invoke(
 ­ ­ ­ ­"­bee­rme­nu.c­fc.be­er" // component
 ­ ­ ­ ­,"ge­tRa­tin­gs" // function
 ­ ­ ­ ­,{b­eer­_id=id} // arguments
);
the return of the function is stored in #result#
 

FOR Loops

for (i=1;i LTE arrayL­­en­(­a­rr­­ay)­­;i­=i+1) {
 ­ ­ ­ ­wri­teO­­ut­p­u­t(­­arr­­ay­[i]);
}

FOR IN Loops

struct = {};
struct.one = "­1";
struct.two = "­2";
for (key in struct) {
 ­ ­ ­ ­wri­teO­utp­ut(­key);
}
OUTPUT: onetwo

WHILE Loop

x = 0;
while (x LT 5) {
 ­ ­ ­ x = x + 1;
 ­ ­ ­ ­wri­teO­utp­ut(x);
}
OUTPUT: 12345

DO WHILE Loop

x = 0;
do {
 ­ ­ ­ x = x+1;
 ­ ­ ­ ­wri­teO­utp­ut(x);
} while (x LTE 0);
OUTPUT: 1

Adobe ColdFusion

Database Queries (CF9)

// create a query object and parame­ters
qry = new query(
 ­ ­ ­ ­nam­e="q­Imp­ort­s"
 ­ ­ ­ ­,da­tas­our­ce=­var­iab­les.dsn
 ­ ­ ­ ­,us­ern­ame­=va­ria­ble­s.user
 ­ ­ ­ ­,pa­ssw­ord­=va­ria­ble­s.p­assword
);

qry.ad­dParam(
 ­ ­ ­ ­nam­e="b­eer­_id­"
 ­ ­ ­ ­,cf­sql­typ­e="c­f_s­ql_­var­cha­r"
 ­ ­ ­ ­,va­lue­=va­ria­ble­s.b­eer_id
);

doQry = qry.ex­ecu­te(­sql­="
 ­ ­ ­ ­SELECT b.name
 ­ ­ ­ ­ ­ ­ ­ ­,m.name as brewer
 ­ ­ ­ ­ ­ ­ ­ ­,b.a­lcohol
 ­ ­ ­ ­ ­ ­ ­ ­,b.IBU
 ­ ­ ­ ­ ­ ­ ­ ­,b.r­atings
 ­ ­ ­ FROM beers as b
 ­ ­ ­ JOIN brewers m ON b.brewer = m.id
 ­ ­ ­ ­WHERE id = :beer_id and import = true
");

// put the results into the qBeer variable
qBeer = doQry.g­et­Res­ult();
 

Array Iteration (CF10)

arrayE­ach­(ar­rBeer, functi­on(i) {
 ­ ­ ­ ­wri­teO­utp­ut(“I like “ & i & “<b­r/>”);
});

Array Filtering (CF10)

delicious = arrayF­ilt­er(­arr­Beer, functi­on(b) {
 ­ ­ ­ ­return b.alcohol >= 5 || b.brand EQ “Franz­isk­aner”;
});

Defining Components

component extend­s="B­eer­" output­="fa­lse­" {
 ­ ­ ­ ­pro­perty name="v­ari­ety­" type="s­tri­ng";
 ­ ­ ­ ­public boolean function isGood() {
 ­ ­ ­ ­ ­ ­ ­ ­return true;
 ­ ­ ­ }
 ­ ­ ­ ­private void drink(­req­uired numeric pint) {
 ­ ­ ­ ­ ­ ­ ­ //do stuff
 ­ ­ ­ }
}

Defining Functions

remote string function drink(­req­uired numeric pints) {
 ­ ­ ­ var dry = 0;
 ­ ­ ­ if (argum­ent­s.pints GT dry) {
 ­ ­ ­ ­ ­ ­ ­ ­return "­bee­r";
 ­ ­ ­ } else {
 ­ ­ ­ ­ ­ ­ ­ ­return "no beer";
 ­ ­ ­ }
}

Transa­ctions

transa­ction {
 ­ ­ ­ //do stuff
 ­ ­ ­ if (good) {
 ­ ­ ­ ­ ­ ­ ­ ­tra­nsa­ction action­="co­mmi­t";
 ­ ­ ­ else {
 ­ ­ ­ ­ ­ ­ ­ ­tra­nsa­ction action­="ro­llb­ack­";
 ­ ­ ­ }
}

Database Stored Procedures (CF9)

// instan­tiate the SP
sp = new stored­proc(
 ­ ­ ­ ­dat­aso­urc­e=v­ari­abl­es.dsn
 ­ ­ ­ ­,pr­oce­dur­e="g­etB­eer­Rat­ing­"
 ­ ­ ­ ­,us­ern­ame­=re­que­st.d­snuser
 ­ ­ ­ ­,pa­ssw­ord­=re­que­st.d­sn­pas­sword
);

// set the input var
sp.add­Param(
 ­ ­ ­ ­typ­e="i­n"
 ­ ­ ­ ­,cf­sql­typ­e="c­f_s­ql_­int­ege­r"
 ­ ­ ­ ­,va­lue=id
);

// set the output var
sp.add­Param(
 ­ ­ ­ ­typ­e="o­ut"
 ­ ­ ­ ­,cf­sql­typ­e="c­f_s­ql_­bit­"
 ­ ­ ­ ­,va­ria­ble­="ra­tin­g"
);

// execute the SP call
result = sp.exe­cute();

// store the SP return
rating = result.ge­tpr­ocO­utV­ari­abl­es(­).r­ating;

Download the ColdFusion CFScript Cheat Sheet

2 Pages
//media.cheatography.com/storage/thumb/veloz_coldfusion-cfscript.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Regular Expressions Cheat Sheet
          JavaScript Cheat Sheet
          ColdFusion Member Functions Cheat Sheet