DevLearningTools

๐Ÿšง This site is under active construction โ€” new tools, guides, and pages are added every week.

MODULE 2 ยท LESSON 05

Output (writeOutput() & <cfoutput>)

How to display text, variables, expressions, and HTML in ColdFusion using writeOutput() in CFScript and <cfoutput> in Tag Syntax.

New lessons are added one at a time as the course gets built out โ€” a graded quiz for each lesson is still on the way.

Displaying information is one of the first things every developer learns. Whether you're showing a welcome message, printing a variable, displaying database records, or building an HTML page, your application needs a way to send output to the browser.

ColdFusion provides two primary ways to display content: writeOutput() in CFScript, and <cfoutput> in Tag Syntax. Both produce the same result โ€” the only difference is the syntax you choose.

By the end of this lesson, you'll understand when to use each approach, how to display variables and expressions, and the most common output techniques used in real ColdFusion applications.

Learning Objectives

After completing this lesson, you'll be able to:

  • Display text using writeOutput().
  • Display text using <cfoutput>.
  • Output variables.
  • Output expressions.
  • Combine text with variables.
  • Understand when to use writeOutput() vs <cfoutput>.

What Is Output?

Output is the content sent from the ColdFusion server to the user's browser. For example, when your page displays "Welcome John," ColdFusion generated that text and sent it to the browser.

Without output functions, users wouldn't see anything.

writeOutput() (CFScript)

writeOutput() is a built-in function that displays text or variable values. Syntax: writeOutput(value);

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.

<cfoutput> (Tag Syntax)

In tag-based CFML, output is usually generated using the <cfoutput> tag.

Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.

Outputting Variables

Variables don't automatically appear on the page. You must explicitly output them.

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
NOTE

Inside <cfoutput>, variables must be wrapped with # symbols.

Output Multiple Variables

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.

Output Expressions

You can display the result of calculations directly.

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.

Combine Text and Variables

This is one of the most common tasks.

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.

Display HTML

ColdFusion can output HTML along with dynamic values.

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
NOTE

Both render as an <h2> heading in the browser, not literal <h2> text.

Output Multiple Lines

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.

Output Current Date

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
NOTE

Your actual output will differ, since now() always returns the current date and time.

Real-World Example

CFScript
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output โ€” illustrative only, not a live ColdFusion/Lucee server.

writeOutput() vs <cfoutput>

writeOutput()<cfoutput>
Used in CFScriptUsed in Tag Syntax
FunctionTag
Prints values immediatelyPrints everything inside the tag
Ideal for script-based codeIdeal for tag-based pages
No # requiredVariables require #variable#

Visual Overview

Variable
Choose Output Method
writeOutput() โ€” CFScript<cfoutput> โ€” Tag Syntax
Browser Output

Common Beginner Mistakes

Forgetting # inside <cfoutput>

<cfoutput>name</cfoutput> prints the literal word "name", not the variable's value. You need <cfoutput>#name#</cfoutput> โ€” the # symbols tell ColdFusion to evaluate it as an expression.

Using writeOutput without parentheses

writeOutput "Hello"; is invalid CFScript syntax. It's a function call, so it needs parentheses: writeOutput("Hello");.

Forgetting to concatenate text

writeOutput("Hello" name); is a syntax error. Join the string and variable with &: writeOutput("Hello " & name);.

Mixing Script and Tag Syntax incorrectly

Writing <cfoutput> directly inside a <cfscript> block doesn't work โ€” <cfoutput> is a tag, and CFScript is script code. Keep CFScript and Tag Syntax separate unless you intentionally switch contexts between them.

Best Practices

  • Prefer writeOutput() when writing CFScript.
  • Prefer <cfoutput> when writing tag-based CFML.
  • Use meaningful variable names.
  • Keep HTML separate from business logic whenever possible.
  • Escape user-generated content before displaying it (covered later in the Security module).

Interview Questions

What is writeOutput()?

A built-in CFScript function used to send output to the browser.

What does <cfoutput> do?

It outputs text, variables, and expressions in tag-based CFML.

Why are # symbols required inside <cfoutput>?

They tell ColdFusion to evaluate the expression instead of treating it as plain text.

Can writeOutput() display variables?

Yes โ€” for example, writeOutput(name); displays the value stored in the name variable.

Which is better: writeOutput() or <cfoutput>?

Neither is better. They produce the same output. Use writeOutput() in CFScript and <cfoutput> in tag-based code.

Summary

In this lesson, you learned how ColdFusion sends content to the browser using writeOutput() in CFScript and <cfoutput> in Tag Syntax. You displayed text, variables, expressions, HTML, dates, and combined multiple values into meaningful output.

Understanding output is essential because nearly every ColdFusion application displays dynamic information to users. Whether you're showing a welcome message, rendering database results, or generating an entire web page, these two output methods are the foundation of dynamic content in ColdFusion.

What's Next?

The next lesson covers Strings โ€” how to create, concatenate, compare, search, split, replace, and manipulate text using ColdFusion's built-in string functions in both CFScript and Tag Syntax.