ColdFusion 9 for ActionScript Developers (CFScript)

coldfusion actionscriptI was very interested to find out that Adobe ColdFusion 9 would allow developers to program everything (I think everything) using CFScript instead of the ColdFusion Markup Language (CFML) which is tag-based and looks very similar to HTML or XML. Being an ActionScript developer I thought it would be awesome if I could use my knowledge of ActionScript to help me learn ColdFusion. Below are just the basics and I hope this information can benefit someone else.

CFScript Block and File Extention

First thing to know is how to setup your files so it will work on a ColdFusion server. 1) Change the file extension to .cfm so it should like something like this: index.cfm. 2) Place your scripts into a <cfscript> CODE HERE </cfscript>. Below will generate a “Hello World” on your website page.

<cfscript>

	writeOutput("Hello World");
	
</cfscript>
Output:

Hello World

CFScript Single & Multi-Line Line Comments

<cfscript>

	// This is a Single Line Comment (Same as ActionScript and other languages) 
	
	/* 
		This is a Multi-Line Comments
		(Same as ActionScript and other languages) 
	*/  

</cfscript>

CFScript Variable

Notice below that we do not use “var” in front of our variables.
We can use “var” in front of a variable if the variable is created within a function. (More on that later)

<cfscript>

	//we just created a variable call "txt".
	txt = "This is sentence one. ";
	
	//we just created another variable call "txtToAdd".
	txtToAdd = "This is sentence two.";
	
	// We use & to join string variables together instead of + in ActionScript.
	writeOutput( txt & txtToAdd );
	
</cfscript>
Output:

This is sentence one. This is sentence two.

Tip: Don’t forget to put a semicolon; at the end of each statement!

CFScript Array

When using ArrayNew() you can create multidimensional arrays by passing in a parameter of 1, 2, or 3. You can also create an array by using the square brackets: [] which I do in the loops section. Note: Arrays in ColdFusion are not 0 based like ActionScript. So arrays in ColdFusion start at 1.

<cfscript>

	firstArray = ArrayNew(1);
	firstArray[1] = "item one";
	firstArray[2] = "item two";
	firstArray[3] = "item three";
	firstArray[4] = "item four";
	
	writeOutput( firstArray[1] );
	
</cfscript>
Output:

item one

Tip: Arrays in ColdFusion are not 0 based. So an array always starts at 1.

CFScript Loops

<cfscript>

	firstArray = ["item one", "item two", "item three", "item four"];
	
	theArrayLength = ArrayLen(firstArray);
				
	// For Loop			
	for( i=1; i <= theArrayLength; i++){
		
		writeOutput( "This is loop " & i & " out of " & theArrayLength);
		writeOutput("<br />");
		
	}
	
	// While Loop
	counter = 1;
	
	while(counter <= theArrayLength){
	
		writeOutput( "This is loop " & counter & " out of " & theArrayLength);
		writeOutput("<br />");
		counter++;
		
	}
	
	// Do While Loop
	counter = 1;
	
	do{
	
		writeOutput( "This is loop " & counter & " out of " & theArrayLength);
		writeOutput("<br />");
		counter++;
		
	}
	
	while(counter <= theArrayLength);
	 
</cfscript>
Output for all loops are the same:

This is loop 1 out of 4
This is loop 2 out of 4
This is loop 3 out of 4
This is loop 4 out of 4

CFScript Statements

<cfscript>

	//If Statement
	expression = 3;
	
	if( expression == 3){
		writeOutput("True, it is equal to 3");
	} else {
		writeOutput("False, it is not equal to 3");
	}
	
</cfscript>
Output:

True, it is equal to 3

<cfscript>

	// Switch Statement
	switchExpression = 3;

	switch (switchExpression) {
		case 0:
			writeOutput(0);
			break;
		case 1:
			writeOutput(1);
			break;
		case 2:
			writeOutput(2);
			break;
		default:
			writeOutput("Not 0, 1, or 2");
	}
	 
</cfscript>
Output:

Not 0, 1, or 2

CFScript Functions

<cfscript>

	// Simple Function
	function cfScriptFunction(){
		writeOutput("Function has be called.");
	}
	
	cfScriptFunction();

</cfscript>
Output:

Function has be called.

<cfscript>

	//Return Function
	function makeSentence(name){
		return name & " is cool.";
	}
	
	newString = makeSentence("Robert");
	
	writeOutput(newString);

</cfscript>
Output:

Robert is cool.

Since I just started playing around with ColdFusion I am not sure of best practices but like ActionScript I think you would want to have a return type on your functions and setting parameters types. Below is what it would look like:

<cfscript>

	// Note: return type "Void" in front of the function
	Void function cfScriptFunction(){
		writeOutput("Function has be called.");
	}
	
	cfScriptFunction();
	
	// Note: the return type "String" in front of the function
	// Note: Parameter "name" has a type as "String". Placed in front of parameter
	String function makeSentence(String name){
		return name & " is cool.";
	}
	
	newString = makeSentence("Robert");
	
	writeOutput(newString);

</cfscript>

Using “var” in front of variables like ActionScript is only allowed inside of a function.

<cfscript>

	message = "I am outside";
	
	function cfScriptFunction(){
		var message = "";
			message = "I am inside";
		writeOutput(message & "<br />");
	}
	
	cfScriptFunction();
	
	writeOutput(message);
	
</cfscript>
Output:

I am inside
I am outside

One Response to “ColdFusion 9 for ActionScript Developers (CFScript)”

  1. Thanks for the clarity here. simple and brief short examples make this easy to digest. I have a question with the case example. Is there a way that cfscript will allow for some type of include? I know that you cannot use cfml tags inside of cfscript but in the switch/case example it would be great to have a way to include cfml templates/files to be outputs. Is that possible?

    I.E.:

    Thanks Again

Leave a Reply