Coldfusion Tutorials



Spry Tutorials

Air Tutorials


Tags, Functions and Variables

Tags, functions and variables are the holy trinity of Coldfusion programming.

You can think of tags as commands that you use to tell the server what to do, function as actions you typically perform upon data and variables as a place to store data in the server's memory.

As I mentioned in the introduction all Coldfusion tags start with cf and typically have start and end tags like:

<cfsometag>
.....
</cfsometag>

And while I was mostly truthful there are some tags that are exceptions to that cardinal rule. For example the <cfset> tag. This tag is used to set variables like:

<cfset firstname= "bob">

Here we have created a variable called firstname with the value of “bob” using the <cfset> tag. We can then use the <cfoutput> tag to output the contents of the variable:

<cfoutput>#firstname#</cfoutput>

Notice again that Coldfusion looks for items surrounded by pound (#) signs and tries to process them as either variables or functions so you must be careful when you mix CFML and HTML together as in the following:

wrong:

<cfoutput><p style="font-color:#ccccc">Hello #firstname# it is so nice to see you</p></cfoutput>

In the above the Coldfusion server will show you an error indicating that it has found an "invalid CFML construct" and give you the line number and column number of where it thinks the error is. This is because it saw the first # sign and didn't find another where it expected to.

Right:

<p style="font-color:#ccccc">Hello <cfoutput>#firstname#</cfoutput> it is so nice to see you</p>

or

<p style="font-color:#ccccc"><cfoutput>Hello #firstname# it is so nice to see you</cfoutput></p>

Mixing all three tags, function, and variables together is what truly lays the full power of Coldfusion in your hands and gives you the tools to create powerful web applications. As you progress through the tutorials you will quickly learn how closely tied the trinity truly are.