Skip to Main Content
Programming ColdFusion MX, 2nd Edition
book

Programming ColdFusion MX, 2nd Edition

by Rob Brooks-Bilson
August 2003
Intermediate to advanced content levelIntermediate to advanced
1140 pages
68h 45m
English
O'Reilly Media, Inc.
Content preview from Programming ColdFusion MX, 2nd Edition

Writing Output

You can’t directly output text or variable values to the browser within a cfscript block. There is, however a function called WriteOutput( ) that you can use to write directly to the page output stream. This technique can output both variable values and plain text from within a cfscript block. Example 19-1 demonstrates the WriteOutput( ) function.

Example 19-1. Using the WriteOutput( ) function within a cfscript block

<h2>Using WriteOutput Within cfscript</h2>
   
<cfscript>
 x = 1;
 y = 2;
 MyMessage = "Hello World";
 
 WriteOutput("x = #x# <br>");
 WriteOutput("MyMessage = #MyMessage# <br>");
 WriteOutput(x+y); 
</cfscript>

In this case, because there is no way to embed HTML directly within the cfscript block, all formatting is done by embedding the HTML code within the WriteOutput( ) function. Executing Example 19-1 results in the output shown in Figure 19-1.

Using WriteOutput( ) within a CFScript block to write to the page output stream

Figure 19-1. Using WriteOutput( ) within a CFScript block to write to the page output stream

It is also possible to output the value of variables set within a cfscript block outside of the cfscript tags. This is accomplished by using a cfoutput block outside the cfscript block to handle the actual output, as shown in Example 19-2.

Example 19-2. Outputting CFScript variables outside the cfscript block

<h2>Using cfoutput To Output Variables Created Within cfscript</h2> <cfscript> x = 1; y = 2; MyMessage = "Hello World"; </cfscript> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming ColdFusion

Programming ColdFusion

Rob Brooks-Bilson

Publisher Resources

ISBN: 0596003803Supplemental ContentErrata Page