|
Comments
|
Today's Top SOA Links
BF on CF Stick to the Script
Stick to the Script
By: Ben Forta
Jun. 22, 2000 12:00 AM
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>? 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 <CFSET fname="Ben"> And here's the same variable assignment using a <CFSCRIPT> block:
<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()> 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> 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>
Conditional Processing
<CFIF Hour(Now()) LT 12> Here's the same code using <CFSCRIPT>:
<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#"> 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>
Looping
<CFLOOP INDEX="i" FROM="1" TO="10"> 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>
Working with Objects 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">
<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. 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!
|
SYS-CON Featured Whitepapers
Most Read This Week |
|||||||||||||||||||||||||||