What Is <cfscript>?
ColdFusion scripting allows you to write portions of your templates with script-based syntax, which is often more concise and straightforward than ColdFusion’s traditional tag-based syntax. While you can’t do everything with <cfscript> that you currently do with tag-based syntax, as you will later see, you may find that writing substantial portions of your code using scripting syntax is more natural for you.
Scripting syntax is very similar to JavaScript syntax with a couple of exceptions that we’ll discuss soon. Listing 44.1 is an example of a tag-based index loop and its <cfscript> counterpart. Try running Listing 44.1 by alternately commenting-out the tag-based version and the <cfscript> version, and you’ll see that each version does the same thing.
Listing 44.1. ScriptingExample.cfm—An Example of ColdFusion Scripting
<!--- Author: Charlie Arehart -- carehart.org ---> <!--- Find cookies other than CFID/CFTOKEN ---> <cfparam name="test" default="1"> <!--- Tag-based structure loop ---> <cfloop collection="#cookie#" item="cname"> <cfif cname neq "cfid" and cname neq "cftoken"> <cfoutput> #cname# <br> </cfoutput> </cfif> </cfloop> <p> <cfscript> // Script-based structure loop for (cname in cookie) { if (cname != "cfid" && cname != "cftoken"){ writeoutput(cname & "<br>"); } } </cfscript>
For many, the scripted loop is more readable and intuitive than the tag-based loop. It uses familiar constructs from other script-based languages (such as // for comments, != for “not equal,” && for “and,” semicolons to end each statement, bracketed loop and if blocks, and not a single pound-sign needed). While this sort of syntax may be foreign to long-time ColdFusion developers, it’s found in many other languages and so makes some developers more comfortable.
Even so, scripting may also appeal to traditional CFML developers, at least in some circumstances. Often, a block of code will simply look cleaner if replaced with a script-based equivalent (a good example is a long list of <cfset> tags.) Similarly, ColdFusion 8’s new implicit array and structure creation tags, though not unique to CFSCRIPT, do add to the improved experience of creating cleaner, more concise CFML code. (Implicit array and structure creation is discussed in Chapter 8, “The Basics of CFML,” in Adobe ColdFusion 8 Web Application Construction Kit, Volume 1: Getting Started.)
As you’ll learn while you read this chapter, ColdFusion scripting has various differences (some advantages and disadvantages) compared to tag-based CFML.
What You Can and Cannot Do with <cfscript>
<cfscript> is a useful alternative to tag-based CFML, but you can’t just replace all your tag-based CFML with scripting because there are things you can do with tags that you can’t do with scripting. For example, scripting can only contain variable assignments, flow-control logic, exception handling, and function calls. You can’t directly query a database, read a file from disk, or perform any other task that requires the use of CFML tags.
The following section explains the differences between <cfscript> and tag-based syntax. In each section, the <cfscript> example is shown first, followed by the tag-based version.
Assignment
myVariable = "Value"; <cfset myVariable = "Value">
Output
Writeoutput(somevar); <cfoutput>#somevar#</cfoutput>
If-Elseif-Else
if(condition1) { [logic]; } else if(condition2) { [logic]; } else { [logic]; } <cfif condition1> [logic] <cfelseif condition2> [logic] <cfelse> [logic] </cfif>
Switch-Case
switch(expression) { case "value1": [logic]; break; case "value2": [logic]; break; default: [logic]; } <cfswitch expression=""> <cfcase value="value1"> [logic] </cfcase> <cfcase value="value2"> [logic] </cfcase> <cfdefaultcase> [logic] </cfdefaultcase> </cfswitch>
For Loops
for(i=1; i <= 10; i++) { [logic]; } <cfloop index="i" from="1" to="10"> [logic] </cfloop>
While Loops
while(expression) { [logic]; } <cfloop condition="#expression#"> [logic]; </cfloop>
Do-While Loops
do { [logic]; } while(expression); (No tag-based equivalent)
For-In Loops
for(key in structure) { [logic]; } <cfloop collection="#structure#" item="key"> [logic] </cfloop>
Array Loops
for(i=1; i <= ArrayLen(array); i++) { [logic] } <cfloop array="#array#" index="currentArrayElement"> [logic] </cfloop>
List Loops
for(i=1; i <= ListLen(list); i++) { [logic] } <cfloop list="#listOfValues#" index="currentListItem"> [logic] </cfloop>
Query Loops
(no script-based equivalent) <cfloop query="queryName"> [logic] </cfloop>
Break
break; <cfbreak>
Continue
continue; (No tag-based equivalent)
Object Processing
cfobject(...); <cfobject ...>
Exception Handling
try { [logic]; } catch(Expression exception) { [logic]; } catch(Any exception) { [logic]; } <cftry> [logic] <cfcatch type="Expression"> [logic]; </cfcatch> <cfcatch type="Any"> [logic]; </cfcatch> </cftry>
Throwing a Custom Exception
(no script-based equivalent) <cfthrow type="BusinessRule.Customer" errorCode="66020" message="Non-wholesale customer" detail="Only registered wholesale customers may place bulk orders.">
Rethrowing a Caught Exception
(no script-based equivalent) <cfrethrow>
Functions
function myFunction(arg1, arg2) { var value = 0; [logic]; return value; } <cffunction name="myFunction" returntype="numeric"> <cfargument name="arg1" type="numeric"> <cfargument name="arg2" type="numeric"> <cfset var value = 0> [logic] <cfreturn value> </cffunction>
Besides the few examples above which indicate no script-based equivalent, there are some other differences between CFML tags and Script-based alternatives, such as with switch-case and try-catch processing, which will be explored later in the sections, “Flow Control Using <cfscript>”and “Exception Handling in <cfscript>,” respectively,
Differences Between <cfscript> and JavaScript
You’ll notice that <cfscript> looks a lot like JavaScript and other familiar scripting languages, but there are some differences compared to those as well. First, a difference compared to JavaScript is that ColdFusion is absolutely fanatical about terminating all statements with a semicolon. Another major difference is that ColdFusion scripting is executed entirely on the server side, as opposed to JavaScript, which is executed entirely in the user’s browser. There is no Document Object Model (DOM) for ColdFusion scripting to access or manipulate, because ColdFusion has no sense of a “document.”
Why Use <cfscript>?
Sure, ColdFusion scripting does offer a few things that tag-based CFML lacks, such as do-while loops and the ability to skip loop iterations using continue. On the other hand, there are just as many if not more things offered by tag-based CFML that ColdFusion scripting lacks. So why use <cfscript> for anything other than its few unique capabilities?
- <cfscript> integrates a familiar, JavaScript-like syntax into ColdFusion.
- <cfscript> provides access to all ColdFusion variables and objects, using a more-intuitive and easier-to-read format.
- <cfscript> supports all ColdFusion functions and introduces a few features and functions not available to its tag-based counterpart.