February 11, 2021
ColdFusion 101: Tags, Script and Functions, Part 1 – Tags
Comments
(0)
February 11, 2021
ColdFusion 101: Tags, Script and Functions, Part 1 – Tags
Newbie 49 posts
Followers: 41 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.

Tags: One of 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 like Javascript 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.  Even the most modern version of HTML, HTML5 uses tags as it’s primary means of creating an HTML document.

Let’s look at the code for a very basic HTML5 document.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        '<title>title</title>
        <link rel="stylesheet" href="style.css">
        <script src="script.js"></script>
    </head>
    <body>
        <!-- page content -->
    </body>
</html>

As you can see, tags start an open angle (less than sign) and end with an end angle (greater than sign).  They have a tag name at the beginning such as body or title.  Some tags have attributes, such as the "charset" attribute of the <meta> tag in the example above.  Some tags need to be closed, with the use of an end tag.  An end tag is an html tag with a backslash before the name.  For example, <body> is a start tag, </body> is the closing tag

And so, HTML, in its most basic form, consists of tags, possibly with attributes, and possibly needing to be closed.

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

<cfoutput>
    <cfloop index="my_index" from="1" to="10">
        <div>The current count is #my_index#.</div>
    </cfloop>
</cfoutput>

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".  That variable is then output on the page each time the loop processes.

Sometimes, tags will need to be closed.  Consider the same code example above.  The <cfloop> tag needs to be closed with the </cfloop> tag in order to tell the ColdFusion Server where to restart the loop.  End tags do not have attributes, and are simply the same tag as the start tag prefixed with a backslash.  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 in a later 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.

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