Somewhere - there is a country - starving - begging - screaming for whitespace. ColdFusion is there for that country. Ok, I've made that joke a few too many times, but it never gets old. (To me.) I was talking with a friend earlier today about ColdFusion and whitespace, and I thought it would be nice to write a quick blog post to cover the main ways to cut down on whitespace. With that, I present the following...

ColdFusion, or how I learned to stop worrying and love whitespace

What follows is a guide to how you can help restrict the whitespace generated by ColdFusion. In a normal request, any and all ColdFusion code will be replaced by whitespace before presented to the user. If loops are involved, the whitespace could grow to a significant amount. Here are ways to help prevent that:

<cfsetting enablecfoutputonly="true">

This is my favorite way to trim whitespace. When used - no output will be returned to the browser unless it is wrapped in <cfoutput> tags. This means you need to wrap everything you want the user to see, even if it doesn't include any ColdFusion variables. While this may mean a bit more overhead for parsing, this is the option I tend to use in my projects.

This tag has it's drawbacks however. First off, it works like a counter. Guess what this code will do:

<cfsetting enablecfoutputonly="true"> <cfsetting enablecfoutputonly="true"> <cfsetting enablecfoutputonly="false"> I've got a backpack. I'm Boba the Fett

You may think it will show the text. However, because cfsetting works like a counter, you have two "points" for true, one one for false. This means the code is the same as:

<cfsetting enablecfoutputonly="true"> I've got a backpack. I'm Boba the Fett

You need to be very careful when using this tag. Typically I place on on the very top of every document, and one at the very bottom. I do not turn it on and leave it on in Application.cfm/cfc. It is extra work, but again, I find this option to be the best for me.

<cfsilent>

The "Nuclear Option" for whitespace protection, this will prevent any output between the start and end tags. Nothing gets out. Period. Here is a quick sample:

<cfsilent> <cfloop index="x" from="1" to="100"> <cf_mooCow dharmaMode="true" value="Swan" /> </cfloop> </cfsilent>

Unlike the <cfsetting> example, even if you have <cfoutput> tags, the content will not be displayed. This is a bit easier to use then the <cfsetting> option.

Output attributes in Functions/CFCs

This is short and sweet. The <cfcomponent> tag supports an output attribute. Set it to false to prevent the "constructor space" (everything outside of methods) from generating whitespace. The <cffunction> tag also supports an output attribute. (And actually has three modes of operation, but don't worry about that.) You should also set it to false unless your CFC method/UDF is returning output. One quick tip (and either Jeff, Scott, or CJ told me this, I think it was Scott), you can cfdump/cfabort in a CFC for debugging and keep the output set to false. Nice.

<cfprocessingdirective suppressWhiteSpace="true">

Just in case you didn't think <cfsetting enablecfoutputonly="true"> was long enough, you can type even more with the <cfprocessingdirective> tag. I recommend that you make a keyboard shortcut if you want to use this tag. Unlike the <cfsetting> tag, this doesn't force you to use <cfoutput> tags, it simply works to cut down on the extra whitespace. Here is a sample:

<cfprocessingdirective suppressWhiteSpace="true"> Hi </cfprocessingdirective>

Note that you use these tags in a "wrapped" mode.

Enable Whitespace Management

For some reason "Whitespace Management" makes me think of Waste Management. Anyway, in the ColdFusion administrator, under the Settings page, there is an option to turn on whitespace management. If turned on, ColdFusion itself will try to trim down on the whitespace it generates. I do not recommend this option. I don't have a big reason not to, but in general, I dislike server wide settings like this. If you rely on it and move your code to someone else's ColdFusion server, you may not be able to use that setting.