CFScript Syntax Guide

The CFScript syntax provides a way of writing CFML code without using tags. CFScript can either be in a script based component (CFC) or inside the cfscript tag.

Exception handling with the cfscript tag

To handle exceptions use try and catch statements, which are equivalent to the cftry and cfcatch tags. For each try statement, you must have a catch statement.

Script support for tags

CF 11+ ColdFusion 11 allows you to invoke nearlly all the builtin tags in a generic manner.

cfexampletag (attrib=1, attr2=2) // The parent tag
    // First child tag having attributes in the parenthesis (Optional)
    cfexamplechild (child_attr1='cv1', child_attr2='cv2')  
        // Nested child tag
        cfexamplegrandchild (name="bob"); 

    // Second child of parent tag
    cfexampleotherchild (child2_attr1='cv1', child2_attr2='cv2');


Note that the attributes of a tag must be enclosed within a parenthesis and also the attributes must be comma-separated.

The script support is not available for the following tags:

<cfscript> (infinite loop :)
<cfoutput> (use writeOutput() instead)
<cfdump> (use writeDump() instead)
<cfinvoke> (use invoke() instead)
<cfobject> (use createObject instead)

Using custom tags in scripts

Custom tags can also be used in cfscript blocks just like builtin ColdFusion tags. This practice is not recommended, use a component instead of a custom tag. But if you really must, you can invoke a custom tag like this:

cf_myCustomTag(myArg="x", otherArg="y");

There are some tags that have multiple implementations

These implementations are due to old syntax or dual syntax (CF9 cfc’s)

//CF9 syntax
thread action="run" name="testName" {
   thread.test = "CFML";
//CF11 syntax
cfthread( action="run" name="testName"){
   thread.test = "CFML";

Examples of CFScript

For Loop

for (i=1; i <=5; i++) {
    // all statements in the block are looped over
    result = i * 2;

While Loop

while (condition) {
    // statements

If / else if / else

count = 10; 
if (count > 20) { 
} else if (count == 8) { 
} else { 

Query Loop

q = queryNew("id,data", "integer,varchar", [  [11, "aa"], [22, "bb"], [33, "cc"]  ] );
for (row in q){
    //result:   1:11:aa;2:22:bb;3:33:cc;

OR for CF 11+

cfloop(query=q, group="fk"){

Resources: CFChef GitHub: Adam Cameron GitHub: Pete Freitag Cheat sheet: Steve Walker CF Tag and Script Equivilents:

Fork me on GitHub