Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
1468 lines (1288 sloc) 38.7 KB

CFML in 100 minutes

ColdFusion Markup Language (CFML) is a great programming language for beginners because it was written to make the programmer's job easy and not care if the computer's job is hard. In this brief introduction we'll look at key language features you need to get started.

  1. Syntax
  2. Variables
  3. Components, Methods, and Parameters
  4. Strings
  5. Numbers
  6. Queries
  7. Arrays
  8. Structures
  9. Conditionals

    1. If, Else If, & Else
    2. Looping
  10. Nothingness & Null

CFML History

CFML is thought of by many as an old programming language, but ColdFusion actually has been continuously improved by Allaire, Macromedia, and now Adobe. The term ColdFusion is often used synonymously with CFML. ColdFusion originated as proprietary technology, however, it is becoming less closed through the availability of competing open source products like Railo and OpenBD . ColdFusion was invented by Jeremy and JJ Allaire in 1995. Their idea for ColdFusion was originally designed to make it easier to connect simple HTML pages to a database, but ColdFusion now includes advanced features for enterprise integration and application development.

When ColdFusion was originally released, it grew an audience quickly in the government and private sector. CFML tag syntax resembles HTML and the CFML script syntax resembles JavaScript. You may want to focus on either the tag or script based examples depending on your comfort level.

And you want to learn CFML so here goes!

1. Syntax

There are two ways to write CFML code. You can use tag or script syntax. For the examples, please focus on one or the other so this tutorial is not confusing. CFML includes a set of instructions you use in pages. You will write one or more instructions in a file then run the file through a CFML engine. Three CFML instructions we will use in this tutorial are 'CFSET', 'CFOUTPUT', and 'CFDUMP'. 'CFSET' is used to create a variable and assign it a value. Also 'CFSET' is used to call methods. 'CFOUTPUT' displays a variable's value. 'CFDUMP' is used to display the contents of simple and complex variables, objects, components, user-defined functions, and other elements.

We might have a file named 'myprogram.cfm' and 'Sample.cfc' like this:

Tag Syntax


<cfset s = New Sample() />


<cffunction name="hello">
<cfreturn "Hello, World!" />

Script Syntax


s = New Sample();


component {
public string function hello(){
return( "Hello, World!" );

For the script example, 'myprogram.cfm' and 'Sample.cfc' would have beginning/closing '' tags around the instructions.

PHP Syntax


$s = new Sample();
echo s->hello();


class Sample
public function hello() {
return "Hello, World!";

Ruby Syntax


class Sample
def hello
"Hello, World!"

require 'Sample.rb'
s =
puts s.hello

2. Variables

Everything needs a name so we can refer to it. A variable, like in math, is just a name for a piece of data. In CFML, variables are very flexible and can be changed at any time. Variables are assigned using a single equals sign ( = ) where the right side of the equals sign is evaluated first, then the value is assigned to the variable named on the left side of the equals.

Go into a CFML file, enter in these example instructions, and observe the output that CFML gives you back:


a = #a#

b = #b#

c = #c#

b = #b#

d = #d#

a = 5;
writeOutput("a = #a#");
b = 10 + 5;
writeOutput("b = #b#");
c = 15 + a + b;
writeOutput("c = #c#");
b = c - a;
writeOutput("b = #b#");
d = "Hello, ";
writeOutput("d = #d#"); 

In this second example, we assume the first example is present.


e = #e#

f = #f#

g = #g#

b = #b#

e = "World!";
writeOutput("e = #e#");
f = d & e;
writeOutput("f = #f#"");
g = d & a & e;
writeOutput("g = #g#");
b = "hi!";
writeOutput("b = #b#");  

*The first few lines in the first example are simple if you've done any programming language before, but the last few get interesting when combining strings and numbers. The code looks a little messy since after each instruction we output a variable.

3. Components, Methods, and Parameters


In CFML, a ColdFusion component (CFC) file contains data and methods. Components are a building blocks for objects. Objects know information, called 'attributes', and can do actions, called 'methods'. In ColdFusion the 'cffunction' tag is used to define methods within a CFC.

For an example of an object, think about you as a human being. You have attributes like height, weight, and eye color. You have methods like walk, run, wash dishes, and daydream. Different kinds of objects have different attributes and methods. In the next sections we'll look at a few specific instructions in CFML.

In CFML we define an object using the 'cfcomponent' instruction and save the file as '.cfc'. Here's an example defining the object type 'PersonalChef.cfc':


component {


Inside the CFC we usually define one or more methods using the 'cffunction' instruction like this:


component {
public string function makeToast(){
makeToast = "Making your toast!";

Inside the 'cffunction' instruction we'd put the code for how the chef should make the toast.

A 'class' is an abstract idea, it defines what all objects of that type can know and do. Think of the chair you're sitting in. Its not an abstract chair, it is an actual chair. We'd call this actual chair an 'instance'. It is a realization of the idea chair. It has measurable attributes like height, color, weight. The class chair, on the other hand, is abstract. The class's weight, color, and size we can't determine them ahead of time.

Once we define a class, we create an instance of that class like this:

frank = New PersonalChef();

We're calling the 'New' instruction on the class 'PersonalChef' and storing it into the variable named 'frank'. Once we have the instance, we can set or get its attributes and call its methods. Methods are called by using this syntax: 'object.method_name()'. So if you have a person named 'frank' you would tell him to make toast by calling 'frank.makeToast()'.

The 'New' instruction creates a new instance of the object and calls it's init () method (if existing). Any arguments supplied to the object will be passed to the init () method. The init () method should return the object instance using '' in order to have the same expected behavior as the 'CreateObject' instruction. If no init () method exists, the object will be returned normally.

Method Parameters

Sometimes methods take one or more parameters telling them how to do what they're suppose to do. For instance, I might call 'frank.makeToast('burned')' for him to burn my toast. Or maybe he has another method where I call 'frank.makebreakfast("toast","eggs")' for him to make both toast and eggs. Parameters can be numbers, strings, or any kind of object. When a method takes a parameter we use the 'cfargument' instruction, it'll look like this:


component {
public string function makeToast(required String color){
makeToast = "Making your toast #arguments.color#!";

The method is requiring us to pass in a 'color' telling it how to do the method 'makeToast'.

Return Value

In CFML, every time you call a method you won't necessarily get a value back. By default, a CFML method returns nothing. We'll talk about nothing and 'null' in the last section of "CFML in 100 minutes". If you called 'makeToast' method above like '' or 'set result = frank.makeToast('burned');', and tried to output 'result' you should have seen 'Variable RESULT is undefined'.

To return data, we use 'cfreturn' to instruct the method to return a 'value'. Since that wasn't in the last instruction before the ending 'cffunction' in your 'makeToast' method, you received nothing and tried to putting that into the 'result' variable.

For the purposes of our next section I'm going to return the chef instance itself from the method. If you wanted to picture the metaphor, imagine you are looking at your chef 'frank'. You say, "Frank, go make my toast", he tells you he's making the toast, goes to make it, then comes back to you to receive more instructions. He's returning himself to you. Here's how we implement it in code:


component {
 public component function makeToast(required String color){
  this.makeToast = "Making your toast #arguments.color#!";
  return this;

4. Strings

In CFML a string is defined as a quote ( '"' ) followed by zero or more letters, numbers, or symbols and followed by another quote ( '"' ). Some simple strings would be '"hello"' or '"This sentence is a string!"'. Strings can be anything from '""', the empty string, to really long sets of text. This whole tutorial, for instance, is stored in a string. Strings have a few important instructions that we'll use.

* 'Len' \ Call 'Len' on a string to get back the number of characters in the string. For instance 'Len("Hello")' would give you back '5'.\ * 'Replace' \ The 'Replace' instruction replaces occurrences of substring1 in a string with substring2, in a specified scope. The search is case sensitive and the scope default is one. For instance, 'Replace("Hello", "e", "")' would give you back '"hllo"' after replacing the first occurrence of '"e"', or 'Replace("Good Morning!", "o", "e", "All")"' would give you '"Geed Merning!"' \ * 'RemoveChars' \ Call 'RemoveChars' to remove characters from a string. For instance, 'RemoveChars("hello bob", 2, 5)' would give you back '"hbob"'. \ * 'Mid' \ The 'mid' instruction extracts a substring from a string. For instance, I could call 'Mid("Welcome to CFML Jumpstart",4,12)' and it would give you back: 'come to CFML'.

Experiment with the following samples in a CFML file:

**Tag** **Script**
#len (tester)\#
#Replace (tester, "o", "e", "All")\#
#RemoveChars (tester, 2, 5)\#


tester = "Good Morning Everyone!";\
writeOutput ("\#len (tester)\#");\
writeOutput (Replace (tester, "o", "e", "All") & "");\
writeOutput (RemoveChars (tester, 2, 5) & "");\
t2 = "sample,data,from,a,CSV";\
t3 = Mid (t2,8,len (t2));\
writeOutput (t3 & "");\

Often a string may store a list like the @t2@ variable in the last example. A string for storing a list isn't the best for performance and usage. Using an array for a list is so much better. We can convert a list into an @array@ using @ListToArray@. We'll discuss arrays in an upcoming section. Try out these next examples in the CFML file assuming we have the code from the last example:



t4 = ListToArray(t3);

The numbers inside the '[]' brackets specify which item of the array you want pulled out. They're numbered starting with 1. So the first example pulls out the '2' array item. This 't4' array contains position '1', the beginning of the list, up to position '4', the ending of the array.

Combining Strings and Variables

It is extremely common that we want to combine the value of a variable with other strings. For instance, lets start with this example string:

'"Happy Saturday!"'

When we put that into the CFML file, it just spits back the same string. If we were writing a proper program we might want it to greet the user when they start the program by saying '"Happy"' then the day of the week. So we can't just put a string like '"Happy Saturday!"' or it'd be saying Saturday even on Tuesday.

What we need to do is combine a variable with the string. There are two ways to do that. The first approach is called string concatenation which is basically just adding strings together:

In the first line we setup a variable to hold the day of the week. Then we'll printed the string Happy combined with the value of the variable 'today' and the string !. You might be thinking, "What was the point of that since we still wrote Saturday in the first line?" Ok, well, if you were writing a real program you'd use CFMLs built-in date instructions like this:

'today = DayOfWeek(Now());'

'Now()' gets the current date and time of the computer running the ColdFusion server. 'DayOfWeek' returns an integer in the range 1 (Sunday) to 7 (Saturday) for the day of the week. We still don't have the day of week as string. Try this:



today = DayOfWeekAsString(DayOfWeek(Now()));
message = "Happy " & today & "!";

Great, no errors and our output looks correct. 'DayOfWeekAsString' did the trick. There is another string combination called string interpolation.

String interpolation is the process of sticking data into the middle of strings. We use the symbols '#' around the 'variable' where in a string the value should be inserted. Inside those hashes we can put any variable and output it in that spot. Our previous example 'message' could be rewritten like this:

message = "Happy #today#!";

If you compare the output you'll see the second example gives the exact same results. The code itself is a little more compact and, personally, I find it much easier to read.

Basically interpolating means evaluate the code inside this '#' wrapper and put it into the string.

5. Numbers

There are two basic kinds of numbers in CFML: integers (whole numbers) and real (numbers with a decimal point). For our workshop, we'll only be dealing with integers. You can use normal math operations with integers including '+', '-', '/', and '*'. The '++' operator can be used to increment a number. It is also the only one we will use to control a loop. We will talk more about Conditional Looping in section 9. Try out this example for the '++' operator:


  #loop# Hello, world!
 I am here!

for (loop = 0 ; loop < 5 ; loop++)
 WriteOutput("#loop# Hello, world!");
WriteOutput("I am here");

In this next example we're using the 'cfloop' instruction with a multiple instructions inside the condition. The CFML script syntax looks for the starting '{' and the ending '}'. Each instruction between the beginning '{' and ending '}' will be executed if the condition is true.

In the tag example there's no need to manage the index inside the loop if you're simply stepping through one item at a time. You can use the 'from' and 'to' arguments, and ColdFusion will simply loop from the first value to the second, and automatically increment the variable in the 'index' argument.

Try this example with multiple instructions:


 #loop# Good Morning! it lunch time yet?

loop = 0; 
while (loop < 5) { 
 WriteOutput("#loop# Good Morning! ");
 WriteOutput(" it lunch time yet?");

It's also possible to go through a loop and step over more than one value at a time. The following examples will step through the loop and increase the 'loop' index by two for each time through the loop.


 #loop# Good Morning! it lunch time yet?

loop = 0;
while (loop < 5) {
 WriteOutput("#loop# Good Morning! ");
 WriteOutput(" it lunch time yet?");
 loop = loop + 2;

6. Queries

A query is a request to a database. The query can ask for information from the database, write new data to the database, update existing information in the database, or delete records from the database. Each time you query a database with CFML, you get the data (the recordset) and the query variables; together they make up the query object. 'cfquery' passes SQL statements to the 'datasource'. The 'datasource' is set in the ColdFusion administrator.

queryService = new Query ();


GetBreakfastItems = queryService.execute().getResult(); 

In order to display the data from our query, we need to loop through the rows, and display each row. This is usually done in a '' tag like so:

 There are #GetBreakfastItems.Quantity# #GetBreakfastItems.Item# in the pantry

While it's not strictly necessary to prepend the recordset name before the column name inside the '', it's strongly recommended that you do in order to prevent referencing the wrong variable scope.

You can also loop through a query using standard loop constructs, though they differ when using tags and script.

 There are #GetBreakfastItems.Quantity# #GetBreakfastItems.Item# in the pantry

for (x = 1; x <= GetBreakfastItems; x=x+1) { 
 writeOutput("There are #GetBreakfastItems.Quantity[x]# #GetBreakfastItems.Item[x]# in the pantry")

When looping through a query with '', you need to make sure that you have a '' tag around your content (or around the loop) to ensure the ColdFusion instructions are recognized.

When looping through a query in 'cfscript', you'll need to reference the query just like you would a multidimensional array, using the counter set up in in your for statement to pick up the correct row from the recordset. So the syntax becomes 'recordsetName.ColumnName[rowNumber]'.

7. Arrays

Often we need to organize a group and put them into a collection. There are two main types of collections: arrays and structures.

An array is a number-indexed list. Picture a city block of houses. Together they form an array and their addresses are the indices. Each house on the block will have a unique address. Some addresses might be empty, but the addresses are all in a specific order. The index is the address of a specific element inside the array. In CFML the index always begins with '1'. An array is defined in CFML as an opening '[' then zero or more elements, and a closing ']'. Try out this code:


favorite_colors = ["red","blue","green","black","brown"];

Keep going with these, but try to understand what each instruction is doing before we explain them:



ArrayAppend(favorite_colors, "orange");
favorite_colors[3] = "yellow";
set = ArraySort(favorite_colors,"text");
ArrayDeleteAt(favorite_colors, 2);

In order to get add an element in the array you use the syntax 'ArrayAppend(array,"value")' or 'arrayname[index] = "value"'. The first example of adding an array element is with an instruction. The second is updating an array element is by assignment. So looking at the final 'favorite_colors' array:

  • What's the index of '"brown"' ?
  • What did the 'ArraySort' instuction do to the collection?
  • What does 'ArrayLen' instruction return?

There are lots of cool things to do with an array. You can rearrange the order of the elements using the 'ArraySort' instruction like we did in the last example. You can iterate through each element using the 'cfloop' instruction. You can find the address of a specific element by using the 'arrayName[index]' instruction. You can ask an array if an element is present with the 'ArrayIsDefined' instruction. Try out this example that brings a bunch of things together:

**Tag** **Script**

\#target\# is \#len (target)\# letters long.

writeOutput ("");\
index = favorite\_colors.iterator ();\
while (index.hasNext ()){\
 target = ();\
 writeOutput ("#target\# is \#len (target)\# letters
writeOutput (""); \
writeDump (var=ArrayIsDefined (favorite\_colors,4));\

We use arrays whenever we need a list where the elements are in a specific order.

8. Structures

A structure is a collection of data where each element of data is addressed by a name. As an analogy, think about a classroom of children. Under ideal circumstances, each student has a name and can be found by using that name. We might look in a science classroom for a child named Joey and that would result in finding an actual student. We could write this like 'science["Joey"]' which could be read as "look in the collection named 'science' and find the thing named 'Joey'".

A structure is an unordered collection, its just a bunch of data collected together where each one has a unique name/key. Structures have a slightly more complicated syntax:

**Tag** **Script**

Joey is \#ages[‘joey']\# years old.\

ages = {jack = 11, brian = 12, tracy = 11};\
ages.joey = 12;\
ages["jill"] = 14;

writeDump (var=ages);\
writeOutput ("Joey is \#ages[‘joey']\# years old.");\

Here we create a structure named 'ages'. Structures are made up what are called key-value pairs.The key is used as the address and the value is the object at that address. In the 'ages' structure we have keys including '"joey"' and '"jill"' and values including '12' and '14'. When creating a structure using '{}' the key and value are linked by the '=' symbol. So to create a structure, the structures start with a curly bracket '{', have zero or more entries made up of a key, '=', and a value separated by commas, then end with a closing curly bracket '}'.

**Tag** **Script**

ages["jimmy"] = 14;\
ages["joey"] = 9;\
writeDump (var=ages);\

In the second chunk of the example, we add a new key and value to the structure. Since the '"jimmy"' key wasn't in the original structure, it's added with the value of '14'. If the key '"jimmy"' already existed then the value would be replaced by '14'. Every key in a structure must be unique! In the second line we reference the key '"joey"' which already exists, so the value gets replaced with the '9'. Then, just to show you the state of the structure, we dump out the list of keys and the list of values.

**Tag** **Script**

 "\#student\# is \#ages[student]\# years

 students = StructSort (ages);

for (i = 1; i LTE ArrayLen (students); i = i+1) {\
 student = students[i];\
 WriteOutput ("\#student\# is \#ages[student]\# years old.");\

The last chunk of the example used StructSort to get the sorted array 'students' from 'ages'. Then, it iterated through the 'students' array using a loop and gave each element of the array the name 'student'. It then printed out one line with that student name and the students age from 'ages'.

While that last part probably seemed complicated, it's just to illustrate that structures are unordered.

9. Conditionals

Conditional statements evaluate to 'true' or 'false' only. The most common conditional operators are '==' (equal), '!=' (not equal), '>' (greater than), '>=' (greater than or equal to), '<' (less than), and '<=' (less than or equal to). You can also define the operators as abbreviations: 'EQ', 'NEQ', 'GT', 'GTE', 'LT', and 'LTE'.

Some instructions return a 'true' or 'false', so they're used in conditional statements. For example, 'IsArray' which is 'true' only when the variable is an 'array'. Structures have an instruction named 'StructKeyExists' which returns 'true' if a key is present in a structure.

9. 1. If, Else If, & Else

Why do we have conditional statements? Most often its to control conditional instructions, especially 'if' / 'else if' / 'else' structures. Lets write an example by adding a method to our 'PersonalChef' class:

**Tag** **Script**


public component function water\_boiling (numeric minutes){\
 if (arguments.minutes < 7) \
 this.status = "The water is not boiling yet.";

else if (arguments.minutes == 7) \
 this.status = "It's just barely boiling.";

else if (arguments.minutes == 8)\
 this.status = "It's

Lastly, when total is 9, it goes:" Is it 'true@ that 9 is less than 7?
No. Next, is it 'true' that 9 is equal to 7? No. Next, is it 'true' that
9 is equal to 8? No. Since none of those are true, execute the 'else'
and print the line 'Hot! Hot! Hot!'.

An 'if' block has

-   One 'if' statement whose instructions are executed only if the
    statement is true
-   Zero or more 'else if' statements whose instructions are executed
    only if the statement is true
-   Zero or one 'else' statement whose instructions are executed if no
    'if' nor 'else if' statements were true

Only *one* section of the 'if' / 'else if' / 'else' structure can have
its instructions run. If the 'if' is 'true', for instance, CFML will
never look at the 'else if'. Once one block executes, thats it.

### 9. 2. Looping

Another time we use conditional statements is when we want to repeat a
set of instructions. Try out this simple example by adding it to your
'PersonalChef.cfc' :




public component function countdown (numeric counter){\
 this.timer = "";\
 while (counter GT 0) { \
 this.timer &= "The counter is \#arguments.counter\#.";\
 return this;\

See how that works? The 'counter' starts out as whatever parameter we
pass in. The 'while' instruction evaluates the conditional statement
'arguments.counter GT 0' and finds that yes, the counter is greater than
zero. Since the condition is true, execute the instructions inside the
loop. First print out '"The counter is #Arguments.counter#"' then take
the value of 'Arguments.counter', subtract one from it, and store it
back into 'Arguments.counter'. Then the loop goes back to the
'condition'/'while' statement. Is it still true? If so, print the line
and subtract one again. Keep repeating until the condition is false.

You can also combine conditional statements using logical operators. The
most common are known as 'logical and' and 'logical or'. In CFML you can
write a 'logical and' with either the word 'and' or with double
ampersands like this: '&&'. You can write a 'logical or' with the word
'or' or with double pipes like this: '||'. For each operation, the
symbolic representation ( '&&' and '||' ) is more common.

The #1 mistake people encounter when writing conditional statements is
the difference between '=' and '=='.

  '=' is an assignment. It means "take what's on the right side and
stick it into whatever is on the left side" (or its telling not
  '==' is a question. It means "is the thing on the right equal to
the thing on the left" (or its asking not telling.)

10. Nothingness & Null

What is nothingness? Is there nothingness only in outer space? Really,
when we think of nothing isn't it just the absence of something? Ok,
that's too much philosophy

ColdFusion did not have a way of referring to nothingness until version
9. ColdFusion can recieve a 'NULL' value from an external source and
maintain the 'NULL' value until you try to use it. ColdFusion will
convert the 'NULL' into an empty string (in the case of queries) or
potentially destroy the variable altogether. However now with greater
support for 'NULL' values, ColdFusion allows you to pass in and return a
'NULL' value from a method. 'IsNull()' instruction will test for 'NULL'
values and return 'true' or 'false'.

If you have three eggs, eat three eggs, then you might think you have
nothing , but in terms of eggs you have '0'. Zero is something, its a
number, and its not nothing.

A large percentage of the errors you encounter while writing CFML code
will involve a variable not existing. You thought something was there,
you tried to do something to it, and you can't do something to nothing
so CFML creates an error. Lets rewrite our 'makeeggs' method to
illustrate 'NULL' :






public component function makeeggs (numeric quantity){\
 if (IsNull (arguments.quantity)) {\
 this.makeEggs = "How am I supposed to make nothingness number of
 } else {\
 this.makeEggs = "Making your \#arguments.quantity\# eggs!";\
 this.yourEggs = ArrayNew (1);\
 while (ArrayLen (this.yourEggs) < arguments.quantity)\
 ArrayAppend (this.yourEggs, "Making an Egg.");\
 return this;\

Reload the file, call 'frank.makeeggs(3)' then try 'frank.makeeggs()'.

TODO: Conclusion
Jump to Line
Something went wrong with that request. Please try again.