CFML consists of two primary language elements: tags and functions. Tags let you perform operations such as accessing a database. Functions can return data and do other operations like retrieving the system date. Almost everything you want to accomplish with ColdFusion will involve using tags and functions.
You will use another important element known as a variable. Variables are an important part of most programming languages and are equally important with CFML. Variables let you store information in memory and enable you to pass data.
The following sections describe how to use these three elements.
You can think of tags as commands that you use to instruct the ColdFusion server to perform operations. These operations might include selecting data from a database, reading a file that resides on the server, or showing the results of processing.
As discussed in Chapter 1, ColdFusion tags are similar to HTML tags. ColdFusion tags are enclosed in angle brackets and often have a start and end tag. The start tag encloses the tag name in brackets, like this:
<tagname>
Most often the end tag encloses the tag name in brackets and includes a slash (/), like this:
</tagname>
The information processed by ColdFusion is placed between the start and end tag, like this:
<tagname>
info to be processed ... </tagname>
ColdFusion tags, for the most part, share these common characteristics:
cf
.
Some ColdFusion tags, such as cfset
, omit the closing tag. This type of tag uses one set of angle brackets and places all the required information between the left (<) and right (>) angle brackets, like this:
<cfset name="bob">
For a complete list of tags and their syntax, see CFML Reference.
Tag attributes instruct the ColdFusion server about the details of an operation. For example, to update a database table, the server needs to know specifics about the database, such as the database name and the table name. The code required to write this type of statement might look like this:
<cfupdate datasource="mydb" tablename="mytable">
where datasource
and tablename
are attributes of the cfupdate
tag and "mydb"
and "mytable"
are attribute values.
For a complete list of tags and their attributes, see CFML Reference.
Typically, a function acts on data. It can generate a value or a set of values, usually from some input. You can perform the following operations (actions) with functions:
Usually, a function performs an operation on a value, and the value can include the value of a variable. For example: to format the value of a variable containing a value in dollars, the code to write this statement might look like this:
#DollarFormat(price)#
The DollarFormat
function returns a value as a string and formats that value with two decimal places, thousand separator, and dollar sign. The pounds signs (#) around the function instruct ColdFusion to evaluate the content between the pound signs and display the value.
All functions have parentheses, regardless of whether the function acts on data. Consider the following function:
#Now()#
If you put anything inside the parentheses of the Now()
function, an error would occur. The Now()
function returns an unformatted date and time. However, you can format the results of this function with other functions, such as the DateFormat()
or TimeFormat()
functions.
Functions can generate data as well as act on data. Consider the following example:
#DateFormat(Now(), "mm/dd/yyyy")#
In this example, the Now()
function generates the date, and then the DateFormat
function formats the date.
You use pound signs (#) with functions to display the results of a function on the page. Pound signs tell the ColdFusion server to evaluate the content between the pound signs and display the value, for example:
<cfoutput>
Hello world, <br> Today's date is #DateFormat(Now(), "mm/dd/yyyy")#
</cfoutput>
The following figure shows the output of this example:
If you did not include the pound signs around the DateFormat(Now(), "mm/ddyyy")
function, the output for the previous example would display as follows:
For more information about how to use pound signs with functions, see Developing ColdFusion MX Applications with CFML.
Variables let you store data in memory on the server. Variables always have a name and a value. You can assign a value to a variable, or you can instruct ColdFusion to assign variable values based on data that it retrieves from a data source, such as a database table.
You must use the following rules for naming ColdFusion variables:
You can use a variable for the following purposes:
ColdFusion lets you create variables as you need them. You create the variable (name and value) using the cfset
tag. The syntax for this tag is:
<cfset variable_name = value>
In the following examples, the variables are assigned a string literal value. All string literal values are surrounded by double quotation marks.
<cfset my_first_name = "Kaleigh">
<cfset my_last_name = "Smith">
In the next example, ColdFusion uses the values of the my_first_name
and my_last_name
variables to set the value for the my_full_name
variable in the last line of code. The ampersand (&
) string operator joins the variables, and the space surrounded by double quotation marks (" "
) adds a space between the variables.
<cfset my_first_name = "Kaleigh">
<cfset my_last_name = "Smith">
<cfset my_full_name = variables.my_first_name & " " & variables.my_last_name>
Tip: String values assigned to a variable must be enclosed in single (') or double (") quotation marks. Numeric or Boolean values assigned to a variable do not require single or double quotation marks.
So far all the variable examples shown have been about local variables. Local variables are variables that you can use only on the current ColdFusion page. As shown in the previous example, a Variables prefix was used to reference an existing variable on the page. Using a prefix when referencing a variable is important because ColdFusion supports many types of variables. The syntax for referencing a local variable is as follows:
variables.variablename
Because ColdFusion lets you use the same name with variables of more than one type, ColdFusion relies on scope referencing. In scope referencing, you preface the variable's name with the scope when you refer to that variable.
ColdFusion supports many types of variables. Each type has it own scope, or where it can be referenced, and its own way of referencing that variable type. The following table identifies some of the more common types of variables and their prefixes:
You will use these other types of variables in Part II of this book. For additional information about variables, see CFML Reference.
Output is what remains after the ColdFusion server processes the CFML tags on a page. Usually the output has two parts:
One of the tags that ColdFusion provides to display output is the cfoutput
tag. The cfoutput
tag instructs ColdFusion to process all the code between the cfoutput
start and end tags. The syntax for the cfoutput
tag looks like this:
<cfouput>
{normal html, text, and coldfusion processing instructions}
</cfoutput>
To return the value of a variable, you must always surround the variable name with pound signs (#) and place the variable name between the cfoutput
start and end tags. For example, the following code creates a variable and instructs the ColdFusion server to return the value of the variable.
<cfset my_first_name = "Kaleigh">
<cfset my_last_name = "Smith"> <cfset my_full_name = variables.my_first_name & " " & variables.my_last_name>
<cfoutput>
#variables.my_full_name#
</cfoutput>