What Is cfscript?


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 few exceptions that we'll discuss soon. Listing 12.1 is an example of a tag-based conditional loop and its <cfscript> counterpart. Try running Listing 12.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 12.1. ScriptingExample.cfmAn Example of ColdFusion Scripting
 <!--- Author: Adam Phillip Churvis -- ProductivityEnhancement.com ---> <!--- Check up to 10 times for existence of the file "file.txt" ---> <!--- Tag-based conditional loop ---> <cfset success = FALSE> <cfset attempts = 0> <cfloop condition="success EQ FALSE AND attempts LT 10">   <cfif FileExists(ExpandPath("file.txt"))>     <cfset success = TRUE>   <cfelse>     <cfset attempts = attempts + 1>   </cfif> </cfloop> <cfscript> // Script-based conditional loop success = FALSE; attempts = 0; while(success EQ FALSE AND attempts LT 10) {   if(FileExists(ExpandPath("file.txt"))) {     success = TRUE;   }   else {     attempts = attempts + 1;   } } </cfscript> <cfoutput>#attempts#</cfoutput> 

For many, the scripted loop is more readable and intuitive than the tag-based loop. As you'll learn while you read this chapter, ColdFusion scripting has some advantages and disadvantages compared to tag-based CFML.

ColdFusion scripting also enables you to create user-defined functions (UDFs), which encapsulate custom logic that can be called just like built-in ColdFusion functions, and which can be reused throughout an application. We'll cover UDFs at the end of this chapter.

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"> 

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 LTE 10; i=i+1) {   [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> 

List Loops
 (no script-based equivalent) <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) 

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> 

By now you probably already know that ColdFusion compiles .cfm and .cfc files into Java classes that are then executed by the Java JVM, and that CFML tags specify the instructions to be carried out by those classes. Think of <cfscript> as just another way to specify a portion of those instructions.

Differences between <cfscript> and JavaScript

You'll notice that <cfscript> looks a lot like JavaScript and other familiar scripting languages, but there are some important differences. First, no angle brackets of any kind (< or >) are allowed within a <cfscript> tag pair, which means you can't use the operators you're accustomed to in JavaScript. You'll have to use ColdFusion operators instead. Second, ColdFusion is absolutely fanatical about terminating all statements with a semicolon. Table 12.2 shows you the major differences between ColdFusion scripting and JavaScript.

Table 12.2. Major Differences Between ColdFusion Scripting and JavaScript

COLDFUSION SCRIPTING

JAVASCRIPT

Semicolon statement termination is mandatory

Semicolon termination is optional in some cases

if(this EQ that) {...}

if(this == that) {...}

if(this NEQ) {...}

if(this <> that) {...}

 

if(this != that) {...}

if(this LT that) {...}

if(this < that) {...}

if(this LTE that) {...}

if(this <= that) {...}

if(this GT that) {...}

if(this > that) {...}

if(this GTE that) {...}

if(this >= that) {...}

if(this MOD that EQ 0)

if(this % that == 0) {...}

i=i+1;

i++;

i=i-1;

i--;

fullName = firstName & lastName;

fullName = firstName + lastName;


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. This is the reason why ColdFusion scripting only looks like JavaScript but lacks most of its familiar statements. 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.



Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net