ColdFusion in Context: Whiteout

No, I'm not advocating that you purchase bottles of white liquid to hide mistakes. This tip demonstrates how to remove extra white space associated with nicely formatted code.

Problem

You don't usually run your tags together like this. It would be hard to read pages and pages full of run-on code like this:

<cfif test condition>do this<cfelseif>do that</cfif>

For clarity, you usually format your code this way:

<cfif test condition>
  do this
<cfelseif>
  do that
</cfif>

However, the extra spaces and linefeeds/carriage returns go to the browser and may be seen by selecting "view source". Due to compression, white space has little impact on download times. However, if you want to remove it, there is a way.

Example

Place all code in whiteout.cfm. Start with this example. It initializes variables that will be used later. It does a simple test to determine if the day is odd or even. The formatting used to make the statements clear can usually be seen.

<hr>
View source to see that the browser normally gets
HTML, text, and white space used in formatting code.
<p>
<!-- This normal comment usually appears --->
<!--- This ColdFusion comment never appears --->
WHAT KIND OF DAY IS TODAY?<br>
<cfset Counter=1>
<cfset Today=now()>
<cfif day(Today) mod 2>
  <cfoutput>Today, #dateFormat(Today)#, is an odd day.</cfoutput>
<cfelse>
  <cfoutput>Today, #dateFormat(Today)#, is an even day.</cfoutput>
</cfif>
<cfoutput>This is example number #Counter#</cfoutput>
<cfset Counter=Counter+1>

Enable ColdFusion Output Only

One solution is to enable only ColdFusion's output: cfoutput tags, cfform's javascript that ColdFusion generates automatically, and the writeOutput() function in cfscript. Except for a gap that seems to be introduced by the writeOutput() function, the ONLY characters passed to the browser are explicitly sent by ColdFusion.

<hr>
The cfsetting tag with enablecfoutputonly set to yes
blocks everything that ColdFusion didn't generate.
This includes the text heading "WHAT KIND...".
The output of cfoutput tags runs together seamlessly.
The output of writeOutput() in a cfscript tag isn't
quite as seamless, and the javascript ColdFusion
generated for the dummy form appears as it normally does.
<p>
<cfsetting enablecfoutputonly="yes">
<!-- This normal comment usually appears --->
<!--- This ColdFusion comment never appears --->
WHAT KIND OF DAY IS TODAY?<br>
<cfif day(Today) mod 2>
  <cfoutput>Today, #dateFormat(Today)#, is an odd day.</cfoutput>
<cfelse>
  <cfoutput>Today, #dateFormat(Today)#, is an even day.</cfoutput>
</cfif>
<cfoutput>This is example number #Counter#</cfoutput>
<cfscript>
writeOutput('!');
</cfscript>
<cfset Counter=Counter+1>
<cfform name="Fred">
<cfinput name="Ignored" type="text" required="yes">
</cfform>
<cfsetting enablecfoutputonly="no">

The Rest Is Silence

Nothing between cfsilent beginning and ending tags will reach the browser.

<hr>
This time, the cfsilent tag has blocked 
all output, even text inside of a cfoutput tag.
The example known as number 3 does not appear.
<p>
<cfsilent>
<!-- This normal comment usually appears --->
<!--- This ColdFusion comment never appears --->
WHAT KIND OF DAY IS TODAY?<br>
<cfif day(Today) mod 2>
  <cfoutput>Today, #dateFormat(Today)#, is an odd day.</cfoutput>
<cfelse>
  <cfoutput>Today, #dateFormat(Today)#, is an even day.</cfoutput>
</cfif>
<cfoutput>This is example number #Counter#</cfoutput>
<cfset Counter=Counter+1>
</cfsilent>

A Little Extra

Of course, the logic used above could be streamlined as in the example below, but the impact of formatting code would not be as obvious for our purpose here. Also, the code below shows a way to suppress debug output.

Suppose you usually display debug output in your development environment but would like to suppress it for specific files. Perhaps these files are part of a frameset and it's hard to see what the overall page should look like when debug information is showing. The cfsetting tag with showdebugoutput set to "no" will take care of this. Note that setting this to "yes" will not automatically display it; settings in ColdFusion Administrator take precedence.

<hr>
Just as a reminder that the odd/even logic
could have been handled by a single function,
look here.
<p>
<cfoutput>Today, #dateFormat(Today)#, is an
#iif(evaluate(day(Today) mod 2),de('odd'),de('even'))#
day.</cfoutput>
<cfoutput>This is example number #Counter#</cfoutput>
<hr>
<cfsetting showdebugoutput="no">
Finally, notice that the debug output many of you
have configured your environment to display
does not appear.  It has been blocked by the cfsetting
tag with showdebugoutput set to "no".

Review

Let's white out white space. If you have a page full of complex logic and little desired output, and the only text you need will come from ColdFusion - you don't need HTML tags, etc. - then cfsetting with enablecfoutputonly can be useful. If you don't want ANYTHING to show, cfsilent can do the job. If you want to suppress debug output, cfsetting with showdebugoutput turned off will help. =Marty=