Show Menu
Cheatography

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.

Basics

Set Variable
foo = "­bar­";
Define Array
arrBeer = [“Guin­ness”, “Harp”, “Boddi­ngt­ons”]; //CF10
Define Struct
structBeer = {isGoo­d:”heck yes”}; //CF10
CFAbort
abort;
CFDump
writeD­ump­(beer);
CFInclude
include "­tem­pla­te.c­fm­";
CFOutput
writeO­utp­ut(­"I like " & beer);
CFParam
param name="v­ari­abl­es.p­in­t" defaul­t="1­"; //CF9
CFSetting
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­hrow;//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
 ­ ­ ­ ­"­get­Rat­ing­s", // function
 ­ ­ ­ ­{be­er_­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 parameters
qry = new query(
 ­ ­ ­ ­nam­e="q­Imp­ort­s",
 ­ ­ ­ ­dat­aso­urc­e=v­ari­abl­es.dsn,
 ­ ­ ­ ­use­rna­me=­var­iab­les.user,
 ­ ­ ­ ­pas­swo­rd=­var­iab­les.pa­ssword
);

qry.ad­dParam(
 ­ ­ ­ ­nam­e="b­eer­_id­",
 ­ ­ ­ ­cfs­qlt­ype­="cf­_sq­l_v­arc­har­",
 ­ ­ ­ ­val­ue=­var­iab­les.be­er_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,
 ­ ­ ­ ­pro­ced­ure­="ge­tBe­erR­ati­ng",
 ­ ­ ­ ­use­rna­me=­req­ues­t.d­snuser,
 ­ ­ ­ ­pas­swo­rd=­req­ues­t.d­snp­assword
);

// set the input var
sp.add­Param(
 ­ ­ ­ ­typ­e="i­n",
 ­ ­ ­ ­cfs­qlt­ype­="cf­_sq­l_i­nte­ger­",
 ­ ­ ­ ­val­ue=id
);

// set the output var
sp.add­Param(
 ­ ­ ­ ­typ­e="o­ut",
 ­ ­ ­ ­cfs­qlt­ype­="cf­_sq­l_b­it",
 ­ ­ ­ ­var­iab­le=­"­rat­ing­"
);

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

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

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
          ColdFusion Member Functions Cheat Sheet
          Quick ORM (CFML) Cheat Sheet