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