Click here to close now.


You will be redirected in 30 seconds or close now.

ColdFusion Authors: Yakov Fain, Maureen O'Gara, Nancy Y. Nee, Tad Anderson, Daniel Kaar

Related Topics: ColdFusion

ColdFusion: Article

A Closer Look at CFScript

A Closer Look at CFScript

Allaire's esteemed guru Ben Forta introduced CFSCRIPT to CFDJ readers last year with his article "Stick to the Script" (CFDJ, Vol. 2, issue 7). Hopefully, some of you absorbed the words of wisdom from our evangelist and gave CFSCRIPT a shot, but I suspect for most of you the lesson fell to the wayside when it came time to hit the code again. I can't blame you. Paltry documentation by Allaire on CFSCRIPT coupled with most CF developers' inexperience in scripting languages makes it easy to ignore.

Most ColdFusion developers don't have a scripting background. So why take up CFSCRIPT? It can't replace conventional CF tag structure. Although a modest performance gain can be seen in some circumstances, its exclusion of CF tags significantly limits its potential functionality. What does it offer most developers who are not seasoned scripters?

I believe it can make three compelling contributions to your repertoire. First, CFSCRIPT simplifies concatenation of strings and basic mathematical manipulation, even if you know little about scripting.

Second, and perhaps most important, CFSCRIPT offers an easy way to encapsulate business logic in a single location at the top of your page. Studio's coloring of scripting helps differentiate business logic from output, making it easy to find and address. It also rests in a more native state, devoid of some of the visual overhead tags create.

The third and rarely discussed reason, at least in my opinion, is that it serves as a great introduction to scripting languages. While most of us enjoy the ease of ColdFusion, we limit ourselves by not employing JavaScript, JSP, or other scripting languages. Indeed, next year Allaire will allow the cohabitation of JSP and ColdFusion in their NEO release (sixth generation ColdFusion). Future ColdFusion developers will really need to know JSP to extract the full potential of future CF applications, and what better way to cut their teeth than inside ColdFusion.

I must confess, jumping into CFSCRIPT may be a bit intimidating. Gone are meaningful plain English tags (CFSET, CFLOOP, etc.) that made developing so easy. Also, as I mentioned earlier, Allaire's normally strong documentation provides woeful coverage of CFSCRIPT. To help you along your daunting journey into the scary forest of scripting, I've prepared a large block of CFSCRIPT employing many of the basic constructs you'll want to use. I've chosen a credit card validation routine for a shopping cart. This doesn't expose all of CFSCRIPT's operations, but should provide enough examples to give you a jump start on your own projects. (Source code for this article can be found on the CFDJ Web site,

To start the process I open with a CFSCRIPT tag. There are no attributes for the tag, but I don't leave the Script block empty (ColdFusion doesn't like this and returns an error). Before I even get to the credit card validation, I'm taking advantage of the fact that I've started a CFSCRIPT block and set defaults for a page that would normally have been accomplished with CFSET or CFPARAM. By moving these sets into the CFSCRIPT, I'm reducing some visual and performance overhead. I identify the section with a comment that I start with two back slashes. These comment markers only comment out a line at a time and have no ending markers to stop the commenting (a bit weird for CF developers, but old hat for scripters).

The first three events are simple sets:

j=0; local.errM = ""; check = "";
(see Figure 1).

Note: Other than comment lines, CFSCRIPT ignores carriage returns. After making each assignment you must place a semicolon before addressing another operation. CFSCRIPT doesn't require any special word to make an assignment (like var or set); simply place a variable on the left, some expression on the right, and end it with a semicolon. Missing semicolons will be the primary cause of errors when you begin CFSCRIPTing, and they're generally not obvious. This single line of code replaces the following three conventional ColdFusion tags:

<CFSET local.errM = "">
<CFSET check = "">
Hopefully, you can recognize the "visual" advantage of making these settings inside of CFSCRIPT in conventional tags.

The next block of code drives navigation links on the page, and again bears no relation to the credit card routine. As I mentioned, you can't use ColdFusion tags inside CFSCRIPT. We can, however, re-create the functionality of some tags using ColdFusion functions. In this case I need to parameterize a form variable, which may or may not have been passed. Since I can't use CFPARAM, I'll have to mimic it with the IsDefined function and a simple assignment if the formfield doesn't exist:

if (Not IsDefined("form.carType")) form.carType = "Mondial t";
This introduces our first conditional statement, a simple if. JavaScripters take note, your favorite operators (!, ==, >=, etcŠ) are not available. You have to use ColdFusion operators (such as "NOT," "IS," "GTE," etc.), which can be a bit frustrating but are necessary nonetheless. To begin the "CFIF" simply type if, followed by the condition you wish to check enclosed in parentheses. In a simple conditional check, place the assignment or action right after the check and end it with a semicolon. In this case, if the condition is met (i.e., there's no form variable), I'd like to set it to a default of "Mondial t". We'll cover the if statements in more detail later, but I wanted to demonstrate that there are workarounds for some ColdFusion tags (although in this case, CFPARAM would have been easier).

Next, I moved some of the logic I employed in the navigation scheme into the CFSCRIPT block. This specific chunk of code occupied eight lines of code in my original template, so it was a prime candidate for moving into this block of CFSCRIPT. Again, we haven't hit the credit card validation yet, I'm just moving logic normally found throughout my page into a clean and more concise environment. This particular snippet peeks at the path to see if we're looking at Ferraris, Maseratis, or Ducatis (see Figure 2). For whichever one the script will set a nav count that will position one of those silly arrows the client absolutely must have:

if (cgi.Script_name CONTAINS "Fer") {local.navCount = 1; local.title = "Ferrari";}
else {if (cgi.Script_Name CONTAINS "Mas") {local.navCount = 16; local.title = "Maserati";}
else {local.navCount = 32; local.title = "Ducati";}}
In CFSCRIPT the braces allow you to perform multiple actions instead of a single set (much the same way parentheses work in ColdFusion or mathematics). Without the braces the if statement would end with the first semicolon. After the first set of braces, we have an else statement (just like CFELSE). I actually needed a CFELSEIF, but that's not directly available in CFSCRIPT so I simply embed another if statement inside the else. This block could have been accomplished with a switch statement (very similar to CFSWITCH), which Ben Forta covered in his CFSCRIPT article.

The final block of code I want to discuss prior to launching into the credit card validation is a simple banner rotator. Again, this logic would normally appear elsewhere on my template, but I'm moving it into the CFSCRIPT block to clean up my page. This gives us an opportunity to witness a simple concatenation and arithmetic operation, which CFSCRIPT performs so well. The snippet takes the title I set in the last section of code and appends a number (from 1 to 6) based on what minute it is. This essentially rotates through six different banners (named, for example, Ferrari1.gif, Ferrari2. gif, etc.) and will be used in the content after the CFSCRIPT block.

variables.mod = local.title & (Minute(now()) MOD 6) & ".gif";
You could extend this to create a string with the link and other HTML features as well. Whenever you have to assemble long strings (such as interaction with a COM object or other third-party software), always consider CFSCRIPT as an alternative to conventional CFSETs. JavaScripters should notice that ampersands, not the addition sign, concatenate strings.

Finally we're ready to play with the credit card validation. The first thing I must do is set some defaults that I'll manipulate further downstream. I could have separated these assignments with a carriage return, but I prefer keeping the simple ones in a single line for cleanliness. Next we see a new comment tag, this time in a multiline format. This comment resembles traditional ColdFusion REM statements in that content contained between the symbols is not executed (versus the single-line comments). To begin a multiline comment we employ a backslash and an asterisk /*; to end we reverse the order and employ an asterisk and a backslash */.

/* Required: local.CardNum, local.expYear, local.expMonth. PASSED: local.errM on exception */
As the comment suggests, we're now going to step through the card number and strip out any nonintegers the client may have provided us. This event gives us our first loop. Scripters will welcome CFSCRIPT's loops, but regular Cold-Fusion developers will probably find these a tad confusing. The looping syntax in CFSCRIPT bears no resemblance to its big brother, CFLOOP, and due to the requirement that CFSCRIPT employs ColdFusion operators, it's not a direct match with JavaScript or Java loops (but is familiarly close).

Loops come in a variety of flavors in CFSCRIPT; the while loop is the first one we'll discuss. It's not the most common loop (which is the for loop), but it's the first we see in this application. The while loop inspects the conditions specified in its parentheses and executes the operations inside its braces as long as the condition is met. This is very similar, yet distinct from a do..while loop, which checks for the condition only after executing each loop, allowing a minimum of one run through the loop. In this scenario we're going to progress through each element of the credit card string the client submitted and discard any characters that are not integers.

while (Len(trim(local.CardNum)) GT 0) { if(IsNumeric(mid(local.CardNum, 1, 1))) check = check & mid(local.CardNum, 1, 1); local.CardNum = RemoveChars(local.CardNum, 1, 1);}

The condition in this situation is the length of the variable we're manipulating. It'll shrink by one position each time through the loop. The trim function discards accidental spaces the client may have provided. As mentioned earlier, the operator must be a ColdFusion operator (in this case "GT") instead of a mathematical or JavaScript one. I have a simple if inquiry that, when true, appends that integer to a new string, cryptically named check. Subsequently, the RemoveChars function strips out the character we had just checked. The same snippet could have been accomplished with a conventional for loop, which I'll discuss soon, or through an REReplace() with regular expressions (a better idea, but it doesn't afford me the opportunity to talk about loops).

The next lines of script build error messages if the card number fails to meet certain basic criteria, specifically length, expiration, and whether the first digit is appropriate for the card type specified. The only thing worth noting here is the compound conditions for the first if statement.

if((Len(local.CardNum) LT 13) OR (Len(local.CardNum) GT 16)) local.errM = local.errM & "<li>Your credit card number must be between 13 and 16 characters long, and should only contain numbers.";
As with its elder brother the CFIF tag, if statements can check for multiple conditions (ranges of values, exclusion of ranges, or different strings).

Before the template evaluates the specifics of the number, it offers an opportunity to short-circuit if any of the earlier conditions uncovers an error. This prevents unnecessary processing of a card we won't accept anyway. To do this I simply wrap the remaining logic in an if statement.

First I perform some quick math and set a couple of defaults. These numbers will be used in the loop that follows.

local.lngth = Len(local.CardNum) - 1; tempCard = Left(local.CardNum,local.lngth); newNum = "";
The local.lngth is one less than the number of integers in the card, and tempCard is the card number stripped of the last digit. CFSCRIPT's variables are case insensitive, but minding case only helps build the coding discipline required in JSP or Java. Many developers prefer to employ more descriptive variables, but I've chosen to err on the side of brevity, due to the math I want to perform. For the same reason I've left the scope off tempCard, newNum, and Step1. In production I'd scope these variables to prevent possible problems.

The i=1; establishes the variable "i" as the index or incrementing variable. The second expression defines the condition under which the loop should continue (see Figure 3). In this case, as long as "i" is less than or equal to the value of local.lngth, the loop will process its contents. The last expression increments the variable. I need to step by two through the loop, thus I set "i" equal to itself plus 2. If I didn't want the step, I'd use the expression i=i+1. The CFLOOP equivalent of this for loop would be:

<CFLOOP from="1" to="#local.lngth#" step="2">
Since some credit cards are 16 digits while others are 15, I need two different operations with an if..else operation to direct each loop iteration. In the case of Visa, Discover, and MasterCard, I need to double the digit I'm currently viewing (specifically in the "i" position) and concatenate it with the next digit in line.
newNum = newNum & (2 * Mid(tempCard,i,1)) & (Mid(tempCard,nextNum,1));
For the American Express card the next digit in line must be doubled and concatenated to the first digit:
newNum = newNum & Mid(tempCard,i,1) & (2 * Mid(tempCard,nextNum,1));

Once I've built the new number I must sum all of its digits. For this a simple for loop can iterate through each number and add it to a variable I'll call sumNum:

for(j=1; j LTE Len(newNum); j=j+1) sumNum = sumNum + Int(Mid(newNum,j,1));
Given the simplicity of the operation, I've left the braces out of this for loop. If you perform more than one operation, however, you'll need braces (just as with if..else statements).

The final step compares the last digit of the credit card against the difference of the right-most digit of sumNum from 10. If it's not a match, I create an error message to display in the content.

In a real application this template would allow the client an opportunity to correct a mistaken entry before sending the card to a financial institution for verification. This template doesn't guarantee that the card submitted is valid, but that it conforms to industry standards. Performing such checks before interacting with third-party agents not only en-hances server performance (by preventing HTTP interaction for an event that was bound to fail), it also improves your chances of completing the transaction by providing the client with an opportunity to immediately correct a mistake. For best results this should be combined with a similar routine in JavaScript, providing instant feedback (if the client has JavaScript turned on) when the client has made a faulty entry.

This same application logic in traditional ColdFusion tags occupies more than twice the number of lines as its CFSCRIPT counterpart and is more difficult to read. By translating the functionality into CFSCRIPT I've made the business logic more transparent, transportable, and refined. Indeed, when I translated this logic, I uncovered several unnecessary redundancies and found a better progression for the logic simply because the operations were unencumbered by tags.

For those ColdFusion developers without scripting experience, I'd highly recommend some experimentation with CFSCRIPT in your next application. As a rule of thumb, anytime more than three consecutive CFSETs lie in a row, you'll benefit from moving them into a CFSCRIPT block. Start slow, making simple assignments in your templates. Next, for giggles, move a loop into CFSCRIPT. Finally, go back to some old business logic module and translate it into CFSCRIPT. Not only will your code be improved, you'll have gained invaluable experience in an important language family: scripting.


More Stories By Christopher Graves

Christopher Graves is president of RapidCF, a ColdFusion development
shop in Connecticut. In his prior "life" he was a Marine Corps
officer and graduate of the U.S. Naval Academy.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

@ThingsExpo Stories
SYS-CON Events announced today that Vicom Computer Services, Inc., a provider of technology and service solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. They are located at booth #427. Vicom Computer Services, Inc. is a progressive leader in the technology industry for over 30 years. Headquartered in the NY Metropolitan area. Vicom provides products and services based on today’s requirements around Unified Networks, Cloud Computing strategies, Virtualization around Software defined Data Ce...
SYS-CON Events announced today that Tufin, the market-leading provider of Security Policy Orchestration Solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. As the market leader of Security Policy Orchestration, Tufin automates and accelerates network configuration changes while maintaining security and compliance. Tufin's award-winning Orchestration Suite™ gives IT organizations the power and agility to enforce security policy across complex, multi-vendor enterprise networks. With more than 1...
SYS-CON Events announced today that Optimal Design, an Internet of Things solution provider, will exhibit at SYS-CON's Internet of @ThingsExpo, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Optimal Design is an award winning product development firm offering industrial design and engineering services to the consumer, medical, and defense markets.
SYS-CON Events announced today that MangoApps will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY., and the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. MangoApps provides private all-in-one social intranets allowing workers to securely collaborate from anywhere in the world and from any device. Social, mobile, and easy to use. MangoApps has been named a "Market Leader" by Ovum Research and a "Cool Vendor" by Gartner...
SYS-CON Events announced today that Cloudian, Inc., the leading provider of hybrid cloud storage solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Cloudian, Inc., is a Foster City, California - based software company specializing in cloud storage software. The main product is Cloudian, an Amazon S3-compliant cloud object storage platform, the bedrock of cloud computing systems, that enables cloud service providers and enterprises to build reliable, affordable and scalable cloud storage solu...
SYS-CON Events announced today that Gridstore™, the leader in hyper-converged infrastructure purpose-built to optimize Microsoft workloads, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Gridstore™ is the leader in hyper-converged infrastructure purpose-built for Microsoft workloads and designed to accelerate applications in virtualized environments. Gridstore’s hyper-converged infrastructure is the industry’s first all flash version of HyperConverged Appliances that include both compute and storag...
SYS-CON Events announced today that Creative Business Solutions will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Creative Business Solutions is the top stocking authorized HP Renew Distributor in the U.S. Based out of Long Island, NY, Creative Business Solutions offers a one-stop shop for a diverse range of products including Proliant, Blade and Industry Standard Servers, Networking, Server Options and Care Packs. As a trusted supplier, CBS guarantees quality controlled stock levels thanks to an Auto...
How is unified communications transforming the way businesses operate? In his session at WebRTC Summit, Arvind Rangarajan, Director of Product Marketing at BroadSoft, will discuss how to extend unified communications experience outside the enterprise through WebRTC. He will also review use cases across different industry verticals. Arvind Rangarajan is Director, Product Marketing at BroadSoft. He has over 19 years of experience in the telecommunications industry in various roles such as Software Development, Product Management and Product Marketing, applied across Wireless, Unified Communic...
SYS-CON Events announced today that IDenticard will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. IDenticard™ is the security division of Brady Corp (NYSE: BRC), a $1.5 billion manufacturer of identification products. We have small-company values with the strength and stability of a major corporation. IDenticard offers local sales, support and service to our customers across the United States and Canada. Our partner network encompasses some 300 of the world's leading systems integrators and security s...
What exactly is a cognitive application? In her session at 16th Cloud Expo, Ashley Hathaway, Product Manager at IBM Watson, will look at the services being offered by the IBM Watson Developer Cloud and what that means for developers and Big Data. She'll explore how IBM Watson and its partnerships will continue to grow and help define what it means to be a cognitive service, as well as take a look at the offerings on Bluemix. She will also check out how Watson and the Alchemy API team up to offer disruptive APIs to developers.
The IoT Bootcamp is coming to Cloud Expo | @ThingsExpo on June 9-10 at the Javits Center in New York. Instructor. Registration is now available at Instructor Janakiram MSV previously taught the famously successful Multi-Cloud Bootcamp at Cloud Expo | @ThingsExpo in November in Santa Clara. Now he is expanding the focus to Janakiram is the founder and CTO of Get Cloud Ready Consulting, a niche Cloud Migration and Cloud Operations firm that recently got acquired by Aditi Technologies. He is a Microsoft Regional Director for Hyderabad, India, and one of the f...
The 17th International Cloud Expo has announced that its Call for Papers is open. 17th International Cloud Expo, to be held November 3-5, 2015, at the Santa Clara Convention Center in Santa Clara, CA, brings together Cloud Computing, APM, APIs, Microservices, Security, Big Data, Internet of Things, DevOps and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding business opportunity. Submit your speaking proposal today!
With IoT exploding, massive data will transform businesses with opportunities to monetize almost anything that can be measured. In this C-Level Roundtable Discussion at @ThingsExpo, Brendan O’Brien, Aria Systems Co-founder and Chief Evangelist, will lead an expert panel of consultants, thought leaders and practitioners who will look at these new monetization trends, discuss the implications, and detail lessons learned from their collective experience. Finally, the panel will point the way forward for enterprises who wish to leverage the resulting complex recurring revenue models, adding valu...
SYS-CON Events announced today that Ciqada will exhibit at SYS-CON's @ThingsExpo, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. Ciqada™ makes it easy to connect your products to the Internet. By integrating key components - hardware, servers, dashboards, and mobile apps - into an easy-to-use, configurable system, your products can quickly and securely join the internet of things. With remote monitoring, control, and alert messaging capability, you will meet your customers' needs of tomorrow - today! Ciqada. Let your products take flight. For more inform...
SYS-CON Events announced today that GENBAND, a leading developer of real time communications software solutions, has been named “Silver Sponsor” of SYS-CON's WebRTC Summit, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. The GENBAND team will be on hand to demonstrate their newest product, Kandy. Kandy is a communications Platform-as-a-Service (PaaS) that enables companies to seamlessly integrate more human communications into their Web and mobile applications - creating more engaging experiences for their customers and boosting collaboration and productiv...
SYS-CON Events announced today that BroadSoft, the leading global provider of Unified Communications and Collaboration (UCC) services to operators worldwide, has been named “Gold Sponsor” of SYS-CON's WebRTC Summit, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. BroadSoft is the leading provider of software and services that enable mobile, fixed-line and cable service providers to offer Unified Communications over their Internet Protocol networks. The Company’s core communications platform enables the delivery of a range of enterprise and consumer calling...
VoxImplant has announced full WebRTC support in the newest versions of its Android SDK and iOS SDK. The updated SDKs, which enable audio and video calls on mobile devices, are now compatible with the WebRTC standard to allow any mobile app to communicate with WebRTC-enabled browsers, including Google Chrome, Mozilla Firefox, Opera, and, when available, Microsoft Spartan. The WebRTC-updated SDKs represent VoxImplant's continued leadership in simplifying the development of real-time communications (RTC) services for app developers. VoxImplant (built by Zingaya, the real-time communication servi...
SYS-CON Events announced today that AIC, a leading provider of OEM/ODM server and storage solutions, will exhibit at SYS-CON's 16th International Cloud Expo®, which will take place on June 9-11, 2015, at the Javits Center in New York City, NY. AIC is a leading provider of both standard OTS, off-the-shelf, and OEM/ODM server and storage solutions. With expert in-house design capabilities, validation, manufacturing and production, AIC's broad selection of products are highly flexible and are configurable to any form factor or custom configuration. AIC leads the industry with nearly 20 years of ...
Buzzword alert: Microservices and IoT at a DevOps conference? What could possibly go wrong? Join this panel of experts as they peel away the buzz and discuss the important architectural principles behind implementing IoT solutions for the enterprise. As remote IoT devices and sensors become increasingly intelligent, they become part of our distributed cloud environment, and we must architect and code accordingly. At the very least, you’ll have no problem filling in your buzzword bingo cards.
Internet of Things (IoT) will be a hybrid ecosystem of diverse devices and sensors collaborating with operational and enterprise systems to create the next big application. In their session at @ThingsExpo, Bramh Gupta, founder and CEO of, and Fred Yatzeck, principal architect leading product development at, will discuss how choosing the right middleware and integration strategy from the get-go will enable IoT solution developers to adapt and grow with the industry, while at the same time reduce Time to Market (TTM) by using plug and play capabilities offered by a robust I...