views:

460

answers:

6

what would be the correct way to stop the white space that coldfusion outputs??

i know there is cfcontent and cfoutputonly.... what is the correct way to do that??

+3  A: 

In terms of tags, there is cfsilent

In the administrator there is a setting to 'Enable whitespace management'

Futher reading on cfsilent and cfcontent reset.

Antony
Similarly there is also the <cfsetting enableCFoutputOnly = "yes" > tag
kevink
and <cfprocessingdirective supressWhiteSpace = "true">
Travis
A: 

If neither <cfsilent> nor <cfsetting enablecfoutputonly="yes"> can satisfy you, then you are probably over-engineering this issue.

When you are asking solely out of aesthetic reasons, my recommendation is: Ignore the whitespace, it does not do any harm.

Tomalak
Whitespace is more of a problem than botching clean code.Whitespace before a doctype will cause errors in validation. Seemingly aesthetic, but it's actually a problem if your customers want/require valid sites. Whitespace before or after a plain text web service return can cause inconveniences too. Leading whitespace in an XML return can cause errors such as "content not allowed in prolog." Both of these results would require the user to manipulate the results just to be useable. In my opinion that's a sloppy web service.
Travis
@Travis: Yes, that's true. Still, these particular issues can be solved perfectly with the available methods. The whitespace that results of intermixing CF and HTML is far less of a problem and can be left alone, IMHO.
Tomalak
In most cases yes I agree it can be ignored. Howerver, I've had a few customers that insist on strict validation (on a funny side note, even his newest sites look like something from 1991). Whitespace wreaks havoc when trying to do this; not just before the doctype but all over the document. Most of it isn't because of CF, but because there's whitespace in the code by the coder, see my contribution to the answers.
Travis
A: 

I've found that even using every possible way to eliminate whitespace, your code may still have some unwanted spaces or line breaks. If you're still experiencing this you may need to sacrifice well formated code for desired output.

for example, instead of:

<cfprocessingdirective supressWhiteSpace = "true">
<cfquery ...>
 ...
 ...
 ...
</cfquery>
<cfoutput>
 Welcome to the site #query.userName#
</cfoutput>
</cfprocessingdirective>

You may need to code:

<cfprocessingdirective supressWhiteSpace = "true"><cfquery ...>
 ...
 ...
 ...
</cfquery><cfoutput>Welcome to the site #query.UserName#</cfoutput></cfprocessingdirective>

This isn't CF adding whitespace, but you adding whitespace when formatting your CF.

HTH

Travis
oh my. Nicely formatted code that is readable and easily understood has to be more important than some extra white space in the HTML outputed code. Come on now.
Jay
See the comments in Tomalak's answer. In most cases yes but not when it is absolutely required, Jay. This is a viable solution when whitespace will botch up your system and CF doesn't handle the whitespace YOU enter.Whitespace before a doctype will cause errors in validation. Seemingly aesthetic, but it's actually a problem if your customers want/require valid sites. Whitespace before or after a plain text web service return can cause inconveniences too. Leading whitespace in an XML return can cause errors such as "content not allowed in prolog."
Travis
+2  A: 

You can modify the ColdFusion output by getting access to the ColdFusion Outpout Buffer. James Brown recently demo'd this at our user group meeting (Central Florida Web Developers User Group).

<cfscript>
  out = getPageContext().getOut().getString();
  newOutput = REreplace(out, 'regex', '', 'all');
</cfscript>

A great place to do this would be in Application.cfc onRequestEnd(). Your result could be a single line of HTML which is then sent to the browser. Work with your web server to GZip and you'll cut bandwidth a great deal.

Aaron Greenlee
A: 

Alternatively, You can ensure your entire page is stored within a variable and all this processing is done within cfsilent tags. e.g.

<cfsilent>
    <!-- some coldfusion -->
    <cfsavecontent variable="pageContent">
        <html>
            <!-- some content -->
        </html>
    </cfsavecontent>
    <!-- reformat pageContent if required -->
</cfsilent><cfoutput>#pageContent#</cfoutput>

Finally, you can perform any additional processing after you've generated the pagecontent but before you output it e.g. a regular expression to remove additional whitespace or some code tidying.

Loftx
A: 

Here's a tip if you use CFC.

If you're not expecting your method to generate any output, use output="false" in <cffunction> and <cfcomponent> (not needed only if you're using CF9 script style). This will eliminate a lot of unwanted whitespaces.

Henry