Appendix B: Scripting with the cfscript Tag


ColdFusion MX allows you to write server-side scripts by using a language known as CFScript, which allows you to access the functionality provided by ColdFusion tags by using script syntax. Since it follows the ECMAScript standard, the CFScript syntax is similar to the JavaScript syntax.

An Introduction to ColdFusion Scripting

Code written in CFScript can access all the built-in functions and expressions of ColdFusion. ColdFusion scripting provides these distinct advantages over ColdFusion tags:

  • It does not require the <cfset> tag to declare or set variables. You can use simple assignment statements to set the value of a variable.

  • It allows you to create custom end user-defined functions.

  • It allows you to build flow control structures using conditional statements, such as if, else, and while.

Creating Scripts Using the <cfscript> Tag

To define a CFScript code block on a ColdFusion page, you need to use the <cfscript> tag. You can write CFScript code between the <cfscript> and </cfscript> tags. For example, the following code shows how to write a sample script using CFScript in a ColdFusion page:

 <HTML> <HEAD> </HEAD> <BODY>     <cfscript>       str="Hello world"     </CFSCRIPT> </BODY> </HTML> 

The main constituents of a script written in a scripting language are

  • Variables

  • Expressions

  • Statements

Variables

Variables allow you to create references for storing numeric, alphanumeric, or object data. You don't have to declare a variable explicitly to use it in your ColdFusion script. When you assign a specified value to a variable, ColdFusion sets the data type of the variable automatically. For example, the following code assigns an integer value to a ColdFusion variable:

 <CFScript>     intvar = 2; </CFScript> 

CFScript code can access any variables, including those created using the <cfset> tag and session or application variables that are available in the ColdFusion page.

Expressions

CFScript supports CFML expressions that include CFML operators, such as +, EQ, IS, and CFML functions. You cannot use JavaScript operators, such as == (equality operator), ++ (incremental operator), or - (decremental operator). Expressions in CFScript code can only use CFML operators, such as EQ, LT, or IS. For example, this code uses LT to determine whether the value of a variable is less than 10:

 <CFScript>     intvar = 11;     if (intvar LT 10)     {       WriteOuput("less than 10");     } </CFScript> 

Statements

CFScript includes statements that allow you to

  • Assign values to variables.

  • Perform conditional checking.

  • Build and run loops.

  • Create functions.

Table B.1 lists the common CFScript statements.

Table B.1: Common CFScript Statements

Statement Type

Purpose

Assignment statements

Assigning values to variables in CFScript code.

If - else statement

Executing a statement or block of statements if a specified conditional expression returns a TRUE value. You can also use this statement to execute a statement or block of statements if a specified expression returns a FALSE value.

For statement

Executing a statement or statements a specified number of times.

while loop statement

Executing a statement or statements inside the while block if a specified expression returns a TRUE value. If the expression gives an FALSE value, the next statement is processed.

The following code shows how to assign a value to a variable using an assignment statement:

 <CFScript>     realvar = 11.3;     newvar = 3     newvar = realvar * newvar; </CFScript> 

Displaying Output Using CFScript Code

You can use the <cfoutput> tag in a ColdFusion page to write information to the Web browser. CFScript uses the WriteOutput function to write information to the browser from a script. WriteOutput is similar to document.write() in JavaScript, because both of them can be used to write plain text as well as HTML code to the browser.

Note

You cannot use ColdFusion tags in CFScript code.

For example, this code uses WriteOutput to write the contents of a string variable to the browser:

 <HTML> <HEAD> </HEAD> <BODY> <cfscript>     str="Hello world";     WriteOutput(str & "<br> Welcome to Scripting using CFScript<br>");     WriteOutput(str & "Robert Mckenzie"); </CFSCRIPT> </BODY> </HTML> 




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

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