ColdFusion Cheat Sheet

This is a quick and dirty cheatsheet for Adobe ColdFusion 10+, many snippets can be used on the older version.

ColdFusion CheatSheet

Set Variables

Use cfset to create a variable (if it doesn’t exist) and assign it a value. You can also use it to call functions.

Current time


 

Regular string

 

Integer

 

Concatenate variable and string


 

Array


 

Data structure


 

Print out

Hard coded string


This is a message for you 

Variable

 _same as inspect in ruby_
#name# _same as puts in ruby_ 

Arrays

Arrays don’t start from 0 like in every other language instead they start from 1

Create array literal

 

Constructor

 

Adding elements in an specific index

 

Appending

 

Looping over the array


    #thing#
 

Loops

There are several different types of for and while loops in ColdFusion.
For more info please see the docs for cfloop.

FOR loop

for (i=1;i LTE ArrayLen(array);i=i+1) {
	WriteOutput(array[i]);
} 

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 

FOR IN Loop (Structure)

struct = StructNew();
struct.one = "1";
struct.two = "2";
for (key in struct) {
	WriteOutput(key);
}
//OUTPUTS onetwo 

FOR IN Loop (Array)

cars = ["Ford","Dodge"];
for (car in cars) {
	WriteOutput(car);
}
//OUTPUTS FordDodge 

FOR IN Loop (Query)

cars = QueryNew("make,model",
	"cf_sql_varchar,cf_sql_varchar",
	[["Ford", "T"],["Dodge","30"]]);
for (car in cars) {
	WriteOutput("Model " & car.model);
}
//OUTPUTS Model TModel 30 

Structures

This are like dictionaries in Python or hashes in Ruby

Create structure literal


 

Constructor

 

Adding elements with brackets


 

Adding elements with dot notation


 

Looping over the structure


    #aGuy[data]#: #data#
 

Queries

Use SQL in coldfusion to retrieve data from a database or enter data in it

Query

var queryOptions = { datasource: "appMain" };
var data = queryExecute(
  "SELECT * FROM users", {}, queryOptions
); 

Allocate query result into variable & retrieve info


    SELECT * FROM TestTable
 

Looping over the Query


    
        

myDataAlfa: #firstQ.myDataAlfa# myDataInt: #firstQ.myDataInt#

#firstQ.columnlist#

#firstQ.recordcount#


Logging

Logging stuff

Clear Log

var logDir = expandPath( "/logs/" );
var logs = directoryList(
  path = logDir,
  listInfo = "name",
  filter = "*.log",
  type = "file",
  recurse = "false"
);
for( var log in logs ){
  var fullPath = logDir & log ;
  if( fileExists( fullPath ) ){
    fileDelete( fullPath );
  }
} 

LogBox

component {
  // ...
  function onError( exception ){
   // uLogging error with logbox...
    writeOutput( "Writing the error in log file.." );
    logger.error(
        "An error occured: #exception.message# #exception.detail#"
        exception
    );
    // error page
    include "views/error.cfm";
  }
} 

Security

Security-related settings for Application.cfc file

Lock down APP

// Application.cfc
component {
  this.name = "myApp";
  this.blockedExtForFileUpload = "*";
  this.scriptProtect					 = "all";
  this.sessioncookie = {
    httpOnly: true,
    secure  : true
  };
} 

Error Handling

ColdFusion provides a variety of tools to customize error information and handle errors when they occur.

Try / Catch / Throw / Finally / Rethrow

try {
	throw(message="Oops", detail="xyz");
} catch (any e) {
	WriteOutput("Error: " & e.message);
	rethrow;
} finally {
	WriteOutput("I run even if no error");
} 

OnError Exception

public function onError(required exception, required string eventName)
  {
    var factory = new App.ExceptionFactory();
    var e = factory.getNewException(arguments.eventName, arguments.exception);
    if (e.logError()) {
      var loggingFile = new App.SomeLoggingCfc(arguments.eventName, arguments.exception);
      loggingFile.commitLog();
    }
    if (e.debugError()) {}
    e.throwException();
  }  

Output CF Error details without CFDump


   ...

   

 

Debugging

You can use CFML tags and functions to display or hide debugging and tracing information.

Control Debugging Output

 

Show Query execution time


SELECT * FROM TestTable
 

Log Values to MyAppSilentTrace.log



 

Comments

ColdFusion comments have a similar format to HTML comments but use three dash characters instead of two.

Single line comment

 

Single line comment

mojo = 1; //THIS IS A COMMENT 

Multiline comment

/* This is a comment
	that can span
	multiple lines
*/ 

We need your help!

Do you know a command that we haven’t included in this ColdFusion CheatSheet?

Help us keep the Adobe ColdFusion CheatSheet up-to-date and enrich it by sharing the CF snippets that you know with other programmers.


Share your knowledge