Tutorials

Writing ColdFusion Using CFScript (Page 1 of 2)

ColdFusion Markup Language is a tag-based server-side language. However, there are actually two ways to write ColdFusion code. The most common form of ColdFusion is in the form of tags like HTML, however, there exists another way to write ColdFusion. Less common than writing your code in tags (start tag and an accompanying end tag) is using CFScript. CFScript, similiar to other ECMA-compliant languages such as JavaScript, was first introduced in ColdFusion 4.0 and is seen as a more advanced way of scripting ColdFusion. Let's look at some code examples of CFScript to gain a better understanding of how it differs from the tag-based style of ColdFusion programming. All of your CFScript code must be contained within the <cfscript> start tag and the lt;/cfscript> end tag.

<CFscript>
   //This line illustrates how a comment is added to your cfscript code.
   //The following line illustrates how to set a variable using cfscript.
   myVar1 = "foo";
   
   //You can concatenate two cfscript variables using the & operator.
   myVar2 = "bar";
   myVar3 = myVar1 & myVar2;

//To output the result of the concatenation to the web browser, use the //following statement in place of cfoutput writeOutput('My concatenated variable: ' & myVar3); </CFscript>

Let's take a look at another common programming practice: looping. First, let's look at a tag-based example of looping in ColdFusion.

<cfoutput>
<cfloop from="1" to="10" index="i">
   ##i##
</cfloop>
</cfoutput>
	

The code above will display the numbers 1 through 10 in your browser window all on one line. The code above can be rewritten in CFScript as follows:

<cfscript>
for (i=1; i LTE 10; i=i+1) {
     writeOutput(i);
}
</cfscript>
	
PREVIOUS | NEXT

RSS