Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Computing
Conference & Expo
November 2-4, 2009 NYC
Register Today and SAVE !..
SYS-CON.TV
Today's Top SOA Links


Stick to the Script
Stick to the Script

One of CFML's most misunderstood (and thus least used) features is the <CFSCRIPT> tag and its supporting scripting language. At the request of several readers (yes, I do take requests, and my e-mail address is at the end of the column), this month we'll spend a little time together exploring this mysterious tag.

What Is <CFSCRIPT>?
CFML is a tag-based language. Always has been and always will be. The ability to write complete applications in simple tags has been one of the most important factors in ColdFusion's success, and that won't be changing.

But as much as we love CFML, developers sometimes find tags clumsy. If you've ever had to set a long list of variables one after the other, you'll know what I mean - that long list of <CFSET> tags isn't that easy to type, and is definitely not easy to read. Scripting (the kind of coding you'd use when writing JavaScript) is sometimes a cleaner option for jobs like that.

And that's where <CFSCRIPT> comes into play. Originally conceived as a way to make ColdFusion development more intuitive for script developers (perhaps JavaScript or Perl developers), <CFSCRIPT> provides an alternate coding method for some operations. And it's easy to use: you simply place your script between <CFSCRIPT> and </CFSCRIPT> tags, and ColdFusion will execute it

Before we go any further, there's one important point to note. <CFSCRIPT> isn't a replacement for CFML. It can only be used to execute functions, not tags. And while the variable assignment and flow control tags (<CFSET>, <CFIF>, <CFLOOP>, etc.) have mirrored functionality within <CFSCRIPT>, most tags don't. So you can't execute a SQL statement within a <CFSCRIPT> block, nor can you send an e-mail message or call a custom tag. CFML functions can be called within a <CFSCRIPT> block; CFML tags can't.

But instead of talking about <CFSCRIPT>, let me show you some usage examples.

Variables and Expressions
Earlier I mentioned variable assignment as one use for <CFSCRIPT>. Many developers find this the single most valuable use for <CFSCRIPT>, so we'll start there. Here's a simple variable assignment using <CFSET>:

<CFSET fname="Ben">

And here's the same variable assignment using a <CFSCRIPT> block:

<CFSCRIPT>
fname="Ben";
</CFSCRIPT>

As you can see, within the <CFSCRIPT> block variables can be assigned using standard script assignment operators. Here a variable named fname would be created, just as it would with <CFSET>. In fact, it's a regular CFML variable and can be used even after the </CFSCRIPT> tag, just like any other variable.

Now I'll admit that was a rather feeble example. The <CFSCRIPT> method is actually more code and more complex than the simple assignment. But take a look at this next block of assignments:

<CFSET config=StructNew()>
<CFSET config.ds="store">
<CFSET config.timeout=30>
<CFSET config.colors=StructNew()>
<CFSET config.colors.bg="black">
<CFSET config.colors.text="yellow">
<CFSET config.colors.links="white">

Here a structure is being created and populated, and that structure contains a second structure that is also created and populated. The following is the <CFSCRIPT> version of this code:

<CFSCRIPT>
config=StructNew();
config.ds="store";
config.timeout=30;
config.colors=StructNew();
config.colors.bg="black";
config.colors.text="yellow";
config.colors.links="white";
</CFSCRIPT>

This code is much easier to read, and for many developers it's easier to write too. Within a <CFSCRIPT> block every statement must be terminated with a semicolon, and white space is ignored so you can place as many statements on one line as you need. Other than that, all the rules that apply to regular CFML expressions apply to <CF SCRIPT>: variable prefixes may be used, pound signs should be used only within strings, case is ignored, mathematical and concatenation operations may be included in expressions, and all CFML functions may be used.

I should point out that as tags may not be used within a <CF SCRIPT> block, <CFOUTPUT> may not be used to generate output text. So how could you write output? With a special function named WriteOutput(), as follows:

<CFSCRIPT>
fname="Ben";
WriteOutput("Hello " & fname);
</CFSCRIPT>

Conditional Processing
<CFCSRIPT> can also be used to perform conditional processing (the equivalent of <CFIF> and <CFSWITCH>). Here is a simple if statement implemented using <CFIF> - it sets an appropriate greeting based on the time of day:

<CFIF Hour(Now()) LT 12>
<CFSET greeting="Good morning">
<CFELSEIF Hour(Now()) LT 18>
<CFSET greeting="Good afternoon">
<CFELSE>
<CFSET greeting="Good evening">
</CFIF>

Here's the same code using <CFSCRIPT>:

<CFSCRIPT>
if (Hour(Now()) LT 12)
greeting="Good morning";
else if (Hour(Now()) LT 18)
greeting="Good afternoon";
else
greeting="Good evening";
</CFSCRIPT>

The end result is exactly the same in both code examples - either way, a single variable named greeting is assigned a value.

Case statements can also be written in <CFSCRIPT>. In this next example a variable named status is inspected. Based on its value (0, 1 or 2), three members in a structure named display are assigned values:

<CFSWITCH EXPRESSION="#status#">
<CFCASE VALUE="0">
<CFSET display.status="Available">
<CFSET display.color="Green">
<CFSET display.allow=TRUE>
</CFCASE>
<CFCASE VALUE="1">
<CFSET display.status="Unavailable">
<CFSET display.color="Red">
<CFSET display.allow=FALSE>
</CFCASE>
<CFCASE VALUE="2">
<CFSET display.status="Back Ordered">
<CFSET display.color="Red">
<CFSET display.allow=TRUE>
</CFCASE>
</CFSWITCH>

Now we'll look at the same code block using <CFSCRIPT>. As you can see, it's much easier to read, but the syntax is definitely pickier. The colons, semicolons, parentheses and curly braces must be specified correctly for this to work.

<CFSCRIPT>
switch(status)
{
case "0":
{
display.status="Available";
display.color="Green";
display.allow=TRUE;
break;
}
case "1":
{
display.status="Unavailable";
display.color="Red";
display.allow=FALSE;
break;
}
case "2":
{
display.status="Back Ordered";
display.color="Red";
display.allow=TRUE;
break;
}
}
</CFSCRIPT>

Looping
<CFSCRIPT> can also be used to perform programmatic loops, much like the <CFLOOP> tag (although not as many loop types are supported). I'm not going to show every type of loop here, but let's look at one simple example. This code loops 10 times, each time displaying the loop count:

<CFLOOP INDEX="i" FROM="1" TO="10">
<CFOUTPUT>#i#<BR></CFOUTPUT>
</CFLOOP>

Here's the same code using <CFSCRIPT>. Unlike <CFLOOP>, the loop counter isn't incremented automatically when using <CF SCRIPT> - you have to do that yourself (thus the i=i+1):

<CFSCRIPT>
for (i=1; i LTE 10; i=i+1)
{
WriteOutput(i & "<BR>");
}
</CFSCRIPT>

Working with Objects
<CFSCRIPT> is also invaluable if you work with external components (COM, CORBA or Java). In CFML these are invoked using the <CFOBJECT> tag - fortunately, ColdFusion 4.5 introduced a function equivalent to that tag: the CreateObject() function.

Take a look at this example (an excerpt from a column I wrote on COM integration in CFDJ, Vol. 1, issue 1). Here <CFOBJECT> is used to invoke a COM object, and then a series of <CFSET> calls are made to set properties and invoke a method:

<CFOBJECT ACTION="CREATE" NAME="Chart" CLASS="ASPChart.Chart">
<CFSET Chart.Height=300>
<CFSET Chart.Width=500>
<CFSET temp=Chart.SaveChart()>

Here is the same code in <CFSCRIPT>:

<CFSCRIPT>
chart=CreateObject("COM", "ASPChart.Chart");
chart.Height=300;
chart.Width=500; </CFSCRIPT>

As you can see, if you use COM components in your code (or CORBA or JSP, for that matter), <CFSCRIPT> can dramatically simplify your coding.

.  .  .

I firmly believe that developers should never have to adapt to conform to the products they work with. Rather, good products should be able to adapt to the habits and likes of developers, accommodating them as much as possible.

<CFSCRIPT> provides alternative coding options for developers who are comfortable in scripting style syntax. There are no real pros or cons to using <CFSCRIPT>, and there are no performance implications either (at least none that Allaire has officially published). It's all about choice and personal preference. And if <CFSCRIPT> works for you, go for it.

About Ben Forta
Ben Forta is Adobe's Senior Technical Evangelist. In that capacity he spends a considerable amount of time talking and writing about Adobe products (with an emphasis on ColdFusion and Flex), and providing feedback to help shape the future direction of the products. By the way, if you are not yet a ColdFusion user, you should be. It is an incredible product, and is truly deserving of all the praise it has been receiving. In a prior life he was a ColdFusion customer (he wrote one of the first large high visibility web sites using the product) and was so impressed he ended up working for the company that created it (Allaire). Ben is also the author of books on ColdFusion, SQL, Windows 2000, JSP, WAP, Regular Expressions, and more. Before joining Adobe (well, Allaire actually, and then Macromedia and Allaire merged, and then Adobe bought Macromedia) he helped found a company called Car.com which provides automotive services (buy a car, sell a car, etc) over the Web. Car.com (including Stoneage) is one of the largest automotive web sites out there, was written entirely in ColdFusion, and is now owned by Auto-By-Tel.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE