Sunday, September 14, 2008

cfscript in coldfusion

An Introduction to CFSCRIPT

We all know and love ColdFusion for its easy to use tag-based syntax. So why is there even a cfscript-style syntax? Well, there are a couple of reasons:
• some CFers are already proficient in other langauges, and may feel more comfortable writing program events in a more 'traditional' method.
• some people think that cfscript style syntax is easier to read (more straightforward).
• cfscript exectues faster than a comparable block of tag-based code (i've heard this is even more true in MX).
So what does all of this mean to you? Do you have to use cfscript? Absolutely not. But given the advanatges mentioned above (as well as the ability to more easily write User-Defined Functions in CF 5 and above), you may want to check out cfscript to see what it can do for you.

'Rules' of CFSCRIPT
• Place cfscript code within <cfscript></cfscript> tags.
• No ColdFusion tags can be used in cfscript blocks. Only ColdFusion functions.
• All lines must be terminated with a semi-colon (;).
• While CFSCRIPT shares many characteristics with other scripting languages such as JavaScript, it is still ColdFusion, which means ColdFusion rules/syntax still apply:
o Arrays start at position 1 (as opposed to 0)
o Use the EQ, NEQ, LT, GT (etc) comparison operators (as opposed to =, !=, <, >, etc)
o The ampersand (&) is the concatenation character (as opposed to the plus (+) character).
o CFSCRIPT is case-insensitive...but I always recommend trying to watch your cases in order to promote good programming habits


Ok...that's all well and good. But what does it LOOK like? Fair enough. Let's start very simple. Setting a variable.

myName = 'Charlie';

Not so terrible, eh? :) You can even use CFSCRIPT to output your values:

myName = 'Charlie';
writeOutput('my name is ' & myName);

Excellent. You are well on your way :)

How about looping? Is there looping in CFSCRIPT? You bet your bippy there is ($5 to anyone who can tell me what a bippy is, by the way...i've always wondered).

Looping in CFSCRIPT

1. <cfscript>
2. myList = 'George,Paul,John,Ringo'; // creates the variable myList, which holds 4 names
3. for (i = 1; i LTE listLen(myList); i = i+1) {
4. writeOutput(listGetAt(myList, i) & "<br>");
5. }
6. </cfscript>

Let's go over this line by line, shall we?
(audience responds in unison, 'we shall')
1. Opening our

2. create a variable called myList. Populate with 4 names (George,Paul,John,Ringo). You'll also notice the comment (the // and the text after it). This is a standard single-line script comment (similar in 'regular' CFML to <!--- comment --->). You can also do a multi-line comment, such as:
This is a multi-line comment.
anything within the slash/asterisk - asterisk/slash will be ignored by the CF Server
3. Here's where we get just a little funky. This line is essentially the same as <cfloop from="1" to="#listLen(myList)#"index="i">, otherwise known as a 'for' loop. It's comprised of 3 parts...let's break 'em down to make this easier to decipher:
• i = 0 -- Straightforward enough. Sets a variable i equal to 0.
• i LTE listLen(myList) -- Says to continue the loop while i is less than or equal to (LTE) the length of our 'myList' list (which contains 4 the loop will iterate 4 times).
• i = i+1 -- Increment i by 1 for each iteration (in other scripting languages, you may have seen this as i++...unfortunately, cfscript does not support this notation).
Notice also, how each 'part' is separated by a semi-colon (;). Notice also the open curly-brace ({) at the end of the line. The code that will execute for each iteration of the loop is enclosed in curly braces (we close our curly brace in line 5).
4. Here's our output. As we did in our first example, we use cfscript's writeOutput() function. Nested within that function, is a straightforward listGetAt() function...with a <br> concatenated at the end, so we get 4 lines of output after our looping is finished. Again, notice that we end the line with a semi-colon (;).
5. We're now done with our code inside of the loop, so we close our curly brace.
6. Close our cfscript block and we're done!

Conditionals in CFSCRIPT

You may also want to look at how CFSCRIPT handles conditionals...otherwise known as <cfif><cfelseif><cfelse></cfif>.

Let's take a very basic example...We're going to generate a random number between 1 and 100...and determined if it's in the first quarter (between 1 and 25), second quarter (between 26 and 50), third quarter (between 51 and 75), or fourth quarter (between 76 and 100).

1. <cfscript>
2. myRandomNumber = randRange(1,100);
3. if (myRandomNumber GTE 1 AND myRandomNumber LTE 25) {
4. writeOutput(myRandomNumber & ' is in the first quarter');
5. } else if (myRandomNumber GTE 26 AND myRandomNumber LTE 50) {
6. writeOutput(myRandomNumber & ' is in the second quarter');
7. } else if (myRandomNumber GTE 51 AND myRandomNumber LTE 76) {
8. writeOutput(myRandomNumber & ' is in the third quarter');
9. } else {
10. writeOutput(myRandomNumber & ' is in the fourth quarter');
11. }
12. </cfscript>

...and the obligatory breakdown :)
1. Opening our <cfscript> block
2. Using ColdFusion's randRange() function, we generate a random number between 1 and 100...assinging that value to the variable myRandomNumber.
3. Our first condition. Checks to see if the value of myRandomNumber is between 1 and 25 (inclusive). This uses standard ColdFusion comparsion operators GTE and LTE for 'greater-than-or-equal-to' and 'less-than-or-equal-to'. We end the line with a curly with our loop example above, any code within the curly braces will appear if this condition is met (curly braces are not mandatory if there is only one line of code to execute in the condition...however, I include them here to show syntactically correct structure).
4. This is code that will execute if the condition in line 3 (above) is met. A simple writeOutput() (which you're familiar with by now) showing the actual value of the variable myRandomNumber, and concatenated with a string literal to show that the number is in quarter one.
5. If the previous condition is not met, we specify another condition (checking for the second quarter, or values between 26 and 50 (inclusive)). The logic is the same as in the first condition. The difference to note is the else if. This is different than ColdFusion's tag-based <cfelse>, which is one word.
6. The code to execute if the condition in line 5 (above) is met. Same as line 4.
7. See line 5 :)
8. See line 6 :)
9. Our final condition. We can use a simple else as opposed to the else if. If none of the previous 3 conditions are met, this is the only one such, we do not have to specify 'if myRandomNumber GTE 76 AND myRandomNumber LTE 100', as it is implicit.
10. See line 6 :)
11. Close our one open curly brace.
12. Close our cfscript block and we're done!

That's about all for this lesson. There's much more that you can do with CFSCRIPT that I did not cover here. In addition to FOR loops, you can do CONDITIONAL loops and DO WHILE loops. For conditional processing, you can also use SWITCH/CASE statements instead of (or in addition to) the conditional example above.


jones said...

Nice blog...
visit also coldfusion example

jackob said...

I was searching the Internet and found your awesome blog. I have studies
many Coldfusion books on the market,If read your this site this is very
informative, and your site gives planty of knowledge about Coldfusion.

Thank You
ColdFusion Developer