April 18, 2021
ColdFusion 101: Tags, Script and Functions, Part 2 – Script
Comments
(0)
April 18, 2021
ColdFusion 101: Tags, Script and Functions, Part 2 – Script
Wizard 47 posts
Followers: 24 people
(0)

Preamble

I love ColdFusion.  I’ve been developing websites, applications, and software using ColdFusion for over 20 years.  I’ve struggled with the reputation ColdFusion has, and I’ve watched the customer and developer base dwindle.  I don’t believe that ColdFusion is a dead or dying language, but I believe that bringing new developers into the fold starts with the basics.  We all started programming somewhere, and I believe there is a dearth of “basic training” available for ColdFusion newcomers.  This series is intended to help with that and provide a resource to developers who are just getting started with ColdFusion.

Assumptions

Articles written in this series will assume a few things:

  • You have little to no knowledge or experience building web based applications.
  • You are using a modern version of ColdFusion.  As of the time of this writing, I will assume that new developers and new development will be utilizing ColdFusion 2021 or ColdFusion 2018.
  • As such, please understand that there may be some limitations in older versions.  Consult documentation for the version of ColdFusion you are coding against for compatibility.

ColdFusion Script (or cfscript) – Another building block of CFML

The script syntax of ColdFusion was introduced in ColdFusion 4 and modern CFML script
resembles many other ECMA script based languages, like JavaScript.  ColdFusion scripting syntax does
not use the conventions that tags use.  In many cases, the tag itself is not required.

Consider the following code example:

<cfset my_variable = "foo">

If this code were to be written in a <cfscript> block, it would appear like this:

<cfscript>
    my_variable = "foo";
</cfscript>

The result of these two blocks of code are exactly the same.  A variable gets created a value of “foo”.  Many developers consider script “easier” to read since it contains fewer constructs.  Modern versions of ColdFusion also feature parity between cfscript and the ColdFusion tags.  Consider the following code examples:

<cfloop index=”my_index” from=”1″ to=”10″>

The current value of my_index is #my_index#<br>

</cfloop>

<cfscript>
    cfloop ( index="my_index", from="1", to="10") {
        writeOutput("The current value of my_index is " & my_index & "<br>");
    }

    for (my_index = 1; my_index lte 10; my_index++) {
        writeOutput("The current value of my_index is " & my_index & "<br>");
    }
</cfscript>

These are three loops; a tag based loop, a cfscript based loop written like a ColdFusion tag, and a cfscript based “for” loop.  Each of these loops will produce the same result.  See for yourself.

A note on end of statement semicolons

ColdFusion 2018 introduced a new feature to cfscript.  Semicolons at the end of each statement are optional most of the time.  There are some instances where a semicolon is still required.  Consider the following code example:

<cfscript>
    my_variable = "foo"
</cfscript>

The lack of a semicolon at the end of the variable declaration will not be a problem for ColdFusion 2018 and above.  However, consider the following code example:

<cfscript>
param name=”my_variable” type=”string” default=”foo”;
writeOutput(my_variable)
</cfscript>

Due to the nature of the cfscript param syntax, a semicolon is required to tell the server that the current line is complete and the next statement is ready to begin.  Because ColdFusion still requires that you write code with a semicolon at the end of each statement some of the time, the general consensus in the development community is to continue to end each statement with a semicolon.

More ways to learn ColdFusion

For more information on learning ColdFusion, https://www.learncfinaweek.com is an excellent information source that covers many of the basic concepts of ColdFusion development.

http://trycf.com and http://cffiddle.org are both websites that allow you to execute ColdFusion code without having to run an instance of ColdFusion.

CommandBox from Ortus Solutions is an excellent, free way to spin up and execute ColdFusion code locally.

Happy coding!

0 Comments
Add Comment