Twitter: raymondcamden


Address: Lafayette, LA, USA

ColdFusion Whitespace Options

07-26-2006 33,293 views ColdFusion 33 Comments

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:

view plain print about
1<cfsetting enablecfoutputonly="true">
2<cfsetting enablecfoutputonly="true">
3<cfsetting enablecfoutputonly="false">
4I'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:

view plain print about
1<cfsetting enablecfoutputonly="true">
2I'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:

view plain print about
1<cfsilent>
2<cfloop index="x" from="1" to="100">
3    <cf_mooCow dharmaMode="true" value="Swan" />
4</cfloop>
5</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:

view plain print about
1<cfprocessingdirective suppressWhiteSpace="true">
2Hi
3</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.

33 Comments

  • Commented on 07-26-2006 at 6:43 AM
    Lucas Sherwood blogged about this not that long ago too...

    http://www.thebitbucket.net/weblog/index.cfm/2006/...
    http://www.thebitbucket.net/weblog/index.cfm/2006/...
    http://www.thebitbucket.net/weblog/index.cfm/2006/...
    http://www.thebitbucket.net/weblog/index.cfm/2006/...
  • Commented on 07-26-2006 at 6:47 AM
    I did not realize. Well, the more info out there the better, right?
  • Commented on 07-26-2006 at 6:47 AM
    I wish Adobe would make it so CF code was processed to not throw back any whitespace. I have tried to figure out, is there a time where you would want all the extra whitespace?

    Oh, and it was me who told you about the cfdump/abort trick.
  • Commented on 07-26-2006 at 6:57 AM
    I thought as much.

    From what I understand the issue is that CF can't tell when you INTEND for the white space, so in cases like where your cf is inside PRE tags.
  • Damien McKenna #
    Commented on 07-26-2006 at 8:19 AM
    /me wants a cf_mooCow.
    Moo!!
  • Montanna #
    Commented on 07-26-2006 at 8:39 AM
    "I've got a backpack. I'm Boba the Fett"

    I laughed so hard... now I have Ice Cream all over my Keyboard. Thanks :)
  • Commented on 07-26-2006 at 8:40 AM
    Montanna, you did see this, right?

    http://ray.camdenfamily.com/index.cfm/2006/4/14/My...

    If not, download the track now. Right now!
  • Tony Petruzzi #
    Commented on 07-26-2006 at 8:54 AM
    This also has been posted a bunch of times:

    http://www.servletsuite.com/servlets/trimflt.htm
  • Commented on 07-26-2006 at 8:55 AM
    That's a cool one Tony. Christian Cantrell also had a filter level sample as well. I didn't mention these as I wanted "pure" CFML solution.
  • Montanna #
    Commented on 07-26-2006 at 8:59 AM
    @Ray I didn't mate, just found your site today (Great stuff BTW). Oh god help me, I have work to do you know. :)
  • Michael De Jonghe #
    Commented on 07-26-2006 at 11:37 AM
    I've begun using the CFLOOP tag instead of CFOUTPUT when looping through querys and outputting to XML. No whitespace makes my XML purdy!
  • Commented on 07-26-2006 at 12:17 PM
    What about cfcontent reset="true"?

    This will wipe out any previous output and start over. I find this handy for Mach-II or Model Glue applications where I have a final "layout" template. The framework cues up some views/variables for later output, then my layout page outputs it. At the top of the layout page is my cfcontent tag.
  • Commented on 07-26-2006 at 12:24 PM
    I'm not sure I'd consider that an option... it feels... "wrong" to me... but thanks for sharing it though. :)
  • Commented on 07-26-2006 at 5:11 PM
    One Mach-II application I worked on had SO much whitespace that it killed my soul. I had to do something like this with the layout file:

    <cfset getPageContext().getOut().clearBuffer() />
    <cfsilent>
    <cfsavecontent variable="thisOutput">
    layout goes here
    </cfsavecontent>
    <cfset thisOutput = REReplaceNoCase(thisOutput, "[[:space:]]{2,}[#Chr(13)##Chr(10)#]{2,}", "", "ALL") />
    <cfoutput>#thisOutput#</cfoutput>

    This uses almost no execution time (10ms or so), and doesn't make the baby Jesus cry about all of the completely unnecessary whitespace.
  • Commented on 07-28-2006 at 2:31 AM
    "is there a time where you would want all the extra whitespace?"

    As well as PRE tags already mentioned, there's also TEXTAREA. You'd lose line/para breaks if the spaces were compressed.
  • Gary Fenton #
    Commented on 07-28-2006 at 7:12 AM
    I can only think of two other reasons why you wouldn't want white space.
    1) It looks ugly, perhaps unprofessional, when you view source from the browser.
    2) It can add a few Kb to the page size.

    I've got over the first issue (just a few therapy sessions!) and I activate web server compression wherever possible to fix the second issue. White space compresses down to nothing - almost literally.
  • Dipak #
    Commented on 02-22-2007 at 4:38 PM
    Hi,
    I have a question regarding White space management:
    I have read your and other's suggestion about setting white space management in CF admin, but in our setup environment (WebLogic + ColdFusion 7.0.2), under "settings" I do not have option to turn-on white space management. I also read in one of the blogs to modify neo-runtime.xml with first attribute to "true", and after setting this, still I do not see option in CF Admin.
    Can you tell me what I need to do to have white space management option available in CF Admin?

    Thanks,
    -Dipak
  • Commented on 02-23-2007 at 8:17 AM
    Sorry - no idea on this one.
  • Commented on 05-10-2007 at 12:09 PM
    If you do not see the whitespace restriction on your admin page then I would first check to see how your admin is running the coldfusion service. Are you using Resin Or Apache? If so you have a config file that you can set a flag from false to true in it and that will turn white space suppression on. Hope this helps
  • Commented on 05-10-2007 at 12:21 PM
    If you do not seem to have the check box to turn white space suppression on the you can do the following and this should enable it for you.

    Locate a file called cffusion\lib\neo-runtime.xml
    open this file up either in vi or notepad
    locate the first boolean and change it from false to true

    You will then need to restart the server.

    Good luck.
  • Commented on 01-25-2008 at 12:40 PM
    Another reason to mistrust CF Administrator's "Whitespace Management" setting...

    I have a CF payroll application that needs to be able to export an ASCII payroll data file with very particular needs for which data is present in which character position of any given line. Chars a-b are an employee ID, c-d are a job code, e-f are a pay rate, etc.

    My CFCONTENT export worked great on my local machine and my dev server, but on production, the export was losing or compressing all instances of multiple spaces down to one single space. I was tearing out my hair trying to figure out why... and then I remembered that hateful CF Admin setting. I never use it because I prefer portable solutions, e.g. code-based rather than environment-dependent, whenever possible... so it took me way too long to remember it was there. Sure enough, that was turned off on local and dev, and on on production.

    Unchecking the box fixed my pain, and I share here in case others are losing spaces they really do need and cannot figure out why!
  • Tim #
    Commented on 07-20-2009 at 10:33 AM
    Any solution for the extra linebreaks i'm encountering? These whitespace supression methods aren't having any effect.

    <cfoutput><div id="map#Trim(eventID)#"></cfoutput>
    is resulting in:
    <div id="map
    44">

    which the javascript i'm trying to use to point at that div is obviously not liking.
  • Commented on 07-20-2009 at 10:41 AM
    Afaik, that's impossible. ;) Can I hit this URL myself to see?
  • Tim #
    Commented on 07-20-2009 at 11:06 AM
    Oh boy. I love it when I'm doing the impossible. Code view will show what I'm talking about.

    http://istart-test.iu.edu/istart/controllers/orien...

    There's a new line at every opening # and I don't particularly want any of them.
  • Commented on 07-20-2009 at 11:14 AM
    And you are certain your code looks exactly like how you posted it above? If so - I don't know what to tell you as that makes no sense to me. Do you maybe have caching turned on in the CF Admin?
  • Commented on 08-26-2009 at 2:46 PM
    I had a situation where nothing from the above worked.
    When cfoutput a var as text/xml i got always an entity error because of the whitespace. I "fixed" that using cfcontent with reset right before the cfoutput.
  • Dave Merrill #
    Commented on 02-21-2010 at 8:58 AM
    Ran across this page trying to understand/avoid a strange behavior I hadn't seen/noticed before, where leading newlines are getting removed. Hopefully the code for this simple test will come through ok:

    <cfsetting showdebugoutput="no" enablecfoutputonly="yes">
    <cfprocessingdirective suppressWhiteSpace="false">
    <cfparam name="ta" default="">

    <cfoutput>
    <form method="post" action="" enctype="multipart/form-data"><!--- or application/x-www-form-urlencoded --->
    <textarea name="ta" rows="10" cols="80">#ta#</textarea>
    <input type="submit" value="Submit">
    </form>
    </cfoutput>

    </cfprocessingdirective>


    Put your cursor in the textarea, hit enter, say three times, type "asdf", then hit enter three more times and submit. One of the leading newlines gets stripped; each time you submit, another one goes. Trailing ones remain.

    As you can see, suppressWhiteSpace="false", and it's off in cf admin anyway. This was tested under cf8. Various browsers, same thing.

    Has anyone else seen this? Does anyone know why it's happening, or how to prevent it?
  • Commented on 02-21-2010 at 10:33 AM
    That's interesting. I added this:

    <cfset test = replace(ta, chr(10), "+", "all")>
    <cfset test = replace(test, chr(13), "*", "all")>
    <cfoutput><p>test=#test#</cfoutput>

    and I could clearly see the newlines in front going away with each hit. But only the ones in front. I wonder if this is simply how the textarea field works? Could you a PHP version for example and see what it does?
  • Dave Merrill #
    Commented on 02-21-2010 at 11:29 AM
    Hmmm, when I tried this with a hidden field (default value had the leading newlines, since I couldn't type in there), nothing got stripped, which does point to some textarea behavior.

    But the server doesn't know if the value came from a textarea, text input, hidden field, whatever, right? And I tried this in various browsers with the same result.

    I don't get it.
  • Commented on 02-21-2010 at 10:10 PM
    I don't get it either. That's why I'm curious to see if maybe it's a CF issue - although it doesn't really feel like one to me.
  • sdtacoma #
    Commented on 05-13-2011 at 11:37 AM
    I realize this is an old posting but I am trying to remove extra spaces when dynamically creating a Word doc.

    If all of my code isn't on one line then spaces are introduced and none of the solutions on this page work to stop it.

    Any idea on how to stop those extra spaces from showing in a Word doc?
  • Commented on 05-13-2011 at 4:37 PM
    Did you try the recommendations here? Maybe you can share some of your code via pastebin so we can see what you are doing.
  • Rick Bergen #
    Commented on 06-01-2011 at 3:16 PM
    I'd like to recommend enablecfoutputonly="true" over or in addition to cfsilent in code and output="no" on cffunctions as a best practice. Without knowing the internals, at least in CF8 it seems that it possibly does more to prevent the generation of whitespace instead of just accumulating it, discarding it, and garbage collecting it. This is an important difference if you have any sort of performance or memory concerns. If you are looping character by character through large enough CSV files or row by row through large enough record sets it is often a necessity. Don't forget enablecfoutputonly="false" at the end of each file if you cfinclude for the "counter" reasons explained in the article.

Post Reply

Please refrain from posting large blocks of code as a comment. Use Pastebin or Gists instead. Text wrapped in asterisks (*) will be bold and text wrapped in underscores (_) will be italicized.

Leave this field empty