February 11, 2021
ColdFusion 101: Tags, Script and Functions
Comments
(0)
February 11, 2021
ColdFusion 101: Tags, Script and Functions
Wizard 43 posts
Followers: 23 people
(0)

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.

Tags and Script: The building blocks of CFML

ColdFusion Tags

When ColdFusion was originally released by Allaire Corporation over 25 years ago, the web was not the dynamic experience it is today. Most sites were basic HTML. CSS was a fraction of what it is now and web-based scripting languages were in their infancy.

HTML by definition is a markup language that creates well structured documents. Documents have a head, body, and footer, with portions of text and sections that are defined and organized. HTML’s primary construct to build web pages are called tags.

In the beginning, ColdFusion borrowed this tag based approach to create CFML, the ColdFusion markup language. ColdFusion tags, much like HTML tags, are opened with an open angle (less than sign) and are closed with an end angle (greater than sign). All ColdFusion tags begin with “cf.” For example:

	<cfoutput>
	<cfset>
	<cfparam>
	<cfloop>
	<cfquery>

These are all types of ColdFusion tags. A list of all of the CFML tags can be found at https://cfdocs.org/tags. This original tag-based approach made ColdFusion an easy language to learn. If you understood HTML and how tags worked, you could easily pick up the concept of ColdFusion tags and how they extended HTML with dynamic functionality.

CFML tags, like HTML tags, have attributes. That is to say that additional parameters can be passed to a tag by adding attributes to it as a series of key/value pairs. Consider the following code example:

	<cfloop index="my_index" from="1" to="10">

In this case, the <cfloop> tag has been passed three attributes, for “index”, “from”, and “to”. These attributes tell the cfloop tag to loop over a section of code 10 times, and to place the current number of the loop in a variable called “my_index”.

Sometimes, tags will need to be closed. Consider the following code example:

	<cfloop index="my_index" from="1" to="10">
	</cfloop>

End tags do not have attributes, and are simply the same tag prefixed with a backslash. In this case the end tag of the <cfloop> is required in order to tell the ColdFusion server where to restart the loop. Here are some more examples.

	<cfquery name="get_users">
		SELECT * FROM users
	</cfquery>

The <cfquery> tag executes a SQL statement against a datasource. In this case, the result of that query is stored in a variable called “get_users”. The end </cfquery> tag is needed to close the database query call from ColdFusion. The code between the tags is a SQL statement used to extract records from a database.

	<cfoutput>
		#dateFormat(now(),"long")#
	</cfoutput>

The <cfoutput> tags instructs the ColdFusion server to parse all of the code within it, and produce HTML output. The end </cfoutput> tag is needed to tell the ColdFusion server when to stop parsing the tags within. The code within these tags is a ColdFusion function that displays the current server date, formatted into a human readable format. I will go into more details about functions later in this article.

A note on trailing slashes

Some people will code CFML tags and end each one that does not require an explicit closing tag with a trailing slash.

Consider the following code example:

	<cfset my_variable = "foo" />

Versus the following code example:

	<cfset my_variable = "foo">

This type of code difference is perfectly acceptable and will be parsed exactly the same by the ColdFusion Server. HTML has never required trailing slashes on tags. There was a time when the gold-standard for web development was XHTML, which, to pass a compatibility test, would require the trailing slash on all unclosed HTML tags, but modern web development should not be done in XHTML anyway. HTML5 doesn’t require trailing slashes, and neither does CFML.

ColdFusion Script (or cfscript)

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 example:

	<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.

https://trycf.com/gist/4eb8d9493fcca79baeb2bc5d5a11998a/acf2018

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.

ColdFusion Functions

ColdFusion functions act upon data that you have in your application. Think of a function as something that takes information you give it, processes it, and provides a modified version of that data.

Let’s take a look at some basic ColdFusion functions in action. Consider the following code example:

	<cfoutput>
		#now()#
	</cfoutput>

In ColdFusion, a pound sign (or hashtag depending on what generation you were born into) indicates code that needs to be parsed by the server and it’s output translated before being sent back to the browser. In the code sample above, there is a function that is written between the two pound signs. (Sorry millennials… I’m a gen-X’er and will always refer to them as pound signs.) The now() function gets the current date and time of the computer running the CFML server, so the output of this code will look like this:

	{ts '2021-02-04 04:29:04'}

This isn’t very readable, so let’s apply a function to that function in order to change the result. Consider the following code example:

	<cfoutput>
		  #dateFormat(now(),"long")#
	</cfoutput>

Executing this code will result in the following output:

	February 4, 2021 4:30:41 AM UTC

This is a basic example of how functions are used to manipulate and change data within your application.

These are many different types of ColdFusion functions. A list of all of the CFML functions can be found at https://cfdocs.org/functions.

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