• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 12. ColdFusion Scripting > What Is <cfscript>?

What Is <cfscript>?

ColdFusion scripting allows you to write portions of your templates with script-based syntax, which is often more concise and straightforward than ColdFusion's traditional tag-based syntax. While you can't do everything with <cfscript> that you currently do with tag-based syntax, as you will later see, you may find that writing substantial portions of your code using scripting syntax is more natural for you. Scripting syntax is very similar to JavaScript syntax with a few exceptions that we'll discuss soon. Listing 12.1 is an example of a tag-based conditional loop and its <cfscript> counterpart. Try running Listing 12.1 by alternately commenting-out the tag-based version and the <cfscript> version, and you'll see that each version does the same thing.

Listing 12.1. ScriptingExample.cfm—An Example of ColdFusion Scripting

<!--- Author: Adam Phillip Churvis -- ProductivityEnhancement.com --->
<!--- Check up to 10 times for existence of the file "file.txt" --->

<!--- Tag-based conditional loop --->
<cfset success = FALSE>
<cfset attempts = 0>
<cfloop condition="success EQ FALSE AND attempts LT 10">
  <cfif FileExists(ExpandPath("file.txt"))>
    <cfset success = TRUE>
  <cfelse>
    <cfset attempts = attempts + 1>
  </cfif>
</cfloop>

<cfscript>
// Script-based conditional loop
success = FALSE;
attempts = 0;
while(success EQ FALSE AND attempts LT 10) {
  if(FileExists(ExpandPath("file.txt"))) {
    success = TRUE;
  }
  else {
    attempts = attempts + 1;
  }
}
</cfscript>

<cfoutput>#attempts#</cfoutput>


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint