White Space / Coldfusion
Asked Answered
D

8

17

What would be the correct way to stop the white space that ColdFusion outputs?

I know there is cfcontent and cfsetting enableCFoutputOnly. What is the correct way to do that?

Doriadorian answered 11/2, 2010 at 2:48 Comment(1)
What is the problem your trying to solve? The Additional white space causes no real harm in and of itself, at least not that I know of.Morbilli
D
12

In addition to <cfsilent>, <cfsetting enablecfoutputonly="yes"> and <cfprocessingdirective suppressWhiteSpace = "true"> is <cfcontent reset="true" />. You can delete whitespaces at the beginning of your document with it.

HTML5 document would then start like this:

<cfcontent type="text/html; charset=utf-8" reset="true" /><!doctype html>

XML document:

<cfcontent reset="yes" type="text/xml; charset=utf-8" /><CFOUTPUT>#VariableHoldingXmlDocAsString#</CFOUTPUT>

This way you won't get the "Content is not allowed in prolog"-error for XML docs.

If you are getting unwanted whitespaces from a function use the output-attribute to suppress any output and return your result as string - for example:

<cffunction name="getMyName" access="public" returntype="string" output="no">
  <cfreturn "Seybsen" />
</cffunction>
Dray answered 8/10, 2011 at 7:38 Comment(1)
This (<cfcontent reset="yes" ...) works on OpenBlueDragon (OpenBD) also.Vomiturition
W
6

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.

Warhol answered 11/2, 2010 at 17:23 Comment(0)
T
4

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.

Transcript answered 11/2, 2010 at 3:28 Comment(4)
Similarly there is also the <cfsetting enableCFoutputOnly = "yes" > tagWatercourse
and <cfprocessingdirective supressWhiteSpace = "true">Adrenalin
@Travis - should be ` <cfprocessingdirective suppressWhiteSpace = "true">` i.e. suppress with 2 p'sChristiachristian
@NicCottrell thanks for correcting a 5 year old type-o. Unfortunately edit isn't available on old comments.Adrenalin
R
1

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.

Rimbaud answered 11/2, 2010 at 10:34 Comment(3)
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.Adrenalin
@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.Rimbaud
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.Adrenalin
A
0

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 formatted code for desired output.

for example, instead of:

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

You may need to code:

<cfprocessingdirective suppressWhiteSpace = "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

Adrenalin answered 11/2, 2010 at 11:54 Comment(2)
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.Morbilli
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."Adrenalin
I
0

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.

Iridescent answered 11/2, 2010 at 22:57 Comment(0)
F
0

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.

Fou answered 16/2, 2010 at 2:17 Comment(0)
N
0

If you have access to the server and want to implement it on every page request search for and install trimflt.jar. It's a Java servlet filter that will remove all whitespace and line breaks before sending it off. Drop the jar in the /WEB-INF/lib dir of CF and edit the web.xml file to add the filter. Its configurable as well to remove comments, exclude files or extensions, and preserve specific strings. Been running it for a few years without a problem. A set it and forget it solution.

Nitrification answered 16/12, 2013 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.