Home > Articles > Mobile Application Development & Programming > HTML5 & Web Standards

Using CFScript

  • Print
  • + Share This
CFScript is a script version subset of the ColdFusion language. Using a syntax similar to JavaScript, CFScript allows you to read and set variables, perform critical processing, and loop. Eben Hewitt walks you through the basics in this article.
This article is adapted from the book Core ColdFusion 5 (Prentice Hall, 2001), by Eben Hewitt.
From the author of

From the author of

In addition to writing ColdFusion using tags, you can perform many typical processing tasks using CFScript. CFScript is a script version subset of the ColdFusion language. Using a syntax similar to JavaScript, CFScript allows you to read and set variables, perform conditional processing, and loop.

However, as a subset, there are limitations. To begin with, there is nothing that you can do inside CFScript that you can't do with tags. Until ColdFusion 5, CFScript didn't really extend the functionality of ColdFusion. Now it is an important facet of the language because you must use CFScript to write user-defined functions. Furthermore, it makes the language more accessible for programmers accustomed to other scripting languages, and it speeds up your performance in certain situations.

How Does CFScript Work?

In order to write ColdFusion script, you must surround your script code with a ColdFusion tag, <cfscript>. This tag takes no attributes, and the closing tag is required. The chief limitation of CFScript is that you may not use any ColdFusion tags inside a script block. Variables that you set inside a script block are visible just like any other variable in ColdFusion. You can manipulate data inside the script block, and it is visible to the rest of your request.

Setting Variables and Commenting

You set a variable by simply writing the name of the variable that you wish to set, an equals sign, and the value. If the variable does not exist, ColdFusion creates it. Let's set a simple variable in Listing 1 to see how this works.

Listing 1: Setting a Variable

<!-----first declare that you're about to write
script by using the <cfscript> tag.------>

<!---then create a variable called "drink". 
Set its value to "coffee"----->

<cfscript>

   Drink = "coffee";

</cfscript>

<!---output the variable value------->

<cfoutput>#drink#</cfoutput>

The variable set inside the block is just the same as if you had created it using <cfset>. The output to the browser is coffee. One thing you probably noticed is the presence of a semicolon at the end of the line. Commands must end in a semicolon within CFScript.

There are two ways to create comments inside a script block. To write a one-line comment, you use two slashes:

<cfscript>
// set the drink variable. I am a comment.

   Drink = "coffee";

</cfscript>

In order to create a multiline comment, you use a /* combination, as you would in SQL or Java, like this:

<cfscript>

/* I am a 
   multiline comment */

   Drink = "coffee";

</cfscript>

You will not see a performance benefit to using CFScript to set only one or two variables in this manner. However, once you have to set three or more at once, you will see a performance increase. The reason for this is that ColdFusion does not have to read and interpret each successive <cfset> tag one by one. Instead, the entire <cfscript> block gets passed to the ColdFusion server and interpreted at once. So, you're likely to see a performance benefit

  • + Share This
  • 🔖 Save To Your Account