Writing Output

To output the contents or results of a ColdFusion expression, you use the <cfoutput> tag. cfoutput is a paired tag, which means that it has both start and end tags. cfoutput tells ColdFusion to parse any text found between the tag pairs for variables and expressions that need to be evaluated. We’ll use the cfoutput tag in a variety of ways throughout the book; it is one of the most commonly used CFML tags. For now, let’s focus on how the cfoutput tag outputs simple variable values.

The following example creates a number of variables using cfset tags and then outputs the values of the variables within cfoutput tags:

<!--- Assign values to variables --->
<cfset x = 1>
<cfset y = x+2>
<cfset Name = "Rob">
<cfset z = Name>
<cfset Authenticated = true>
<cfset TheDate = DateFormat(Now( ),'mm/dd/yyyy')>
   
<!--- Output the variable values --->
<h2>Writing Output</h2>
<cfoutput>
x = #x#<br>
y = #y#<br>
Name = #Name#<br>
z = #z#<br>
TheDate = #TheDate#<br>
Authenticated = #Authenticated#<br>
</cfoutput>

Executing this template causes the value assigned to each variable to be output to the browser, as shown in Figure 2-3. Note the use of pound signs (#) in this example. ColdFusion uses pound signs to separate expressions from literal text. When ColdFusion encounters an expression surrounded by pound signs, it attempts to evaluate it.

Writing output using the cfoutput tag

Figure 2-3. Writing output using the cfoutput ...

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.