Edit

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 nearly all the built-in 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)
<cfinvokeargument>
<cfobject> (use createObject instead)

Using custom tags in scripts

Custom tags can also be used in cfscript blocks just like built-in 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;
    writeOutput(result);
}

While Loop

while (condition) {
    // statements
}

If / else if / else

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

Query Loop

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

OR for CF 11+

cfloop(query=q, group="fk") {
    writeOutput("<strong>#fk#</strong>");
}

Break / Continue

for (row in q) {
    if (row.skip) {
        continue;
    }
    //do stuff...
}

for (row in q) {
    if (q.currentRow > 5) {
        break;
    }
    //process rows 1-5
}

Resources

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub