3

Consider test.cfm file with the following content:

<html>
    <body>
        <cfif foo EQ bar>
            <cfset test = "something" />
        </cfif>
        <p>Hello!</p>
    </body>
</html>

When run in the browser, the source code of the output of this file will look like this:

<html>
    <body>



        <p>Hello!</p>
    </body>
</html>

Is there any way to fix this?

Eleeist
  • 6,891
  • 10
  • 50
  • 77

3 Answers3

7

Is there any way to fix this?

There's nothing to fix - the HTML is perfectly valid and functional.

If your issue is the size of request, use gzip encoding.

If your issue is reading the source for debugging/etc, use developer tools such as Firebug/etc.


However, general things you should be doing to improve maintainability (which at the same time also reduces whitespace output) are:

1) Move anything that isn't display logic out of your views.

2) Convert display logic to functions and custom tags as appropriate, which both make it easier to prevent/control output.


To prevent unwanted content being output, you can:

  • Wrap the entire section in cfsilent, to ensure nothing gets output.

  • Enable enablecfoutputonly attribute of cfsetting then only use cfoutput around things you want to be output.

  • Always set output=false on component and function tags.

  • When you want to selectively output some text, wrap non-tag non-output segments in CFML comments <!---...---> (e.g. useful for preventing newline output in custom tags)

(I never bother with cfprocessingdirective, everything mentioned above solves the issues better.)

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
  • 1) Of course all the "business logic" goes into CFC's functions, but I still have to invoke them somehow in the CFMs, don't I? // I will look into gzip encoding, which I think will be the most efficient way to do this... – Eleeist Aug 18 '12 at 20:47
  • 1
    You're confusing terms. Business logic goes where it needs to go. Display logic is when you need to control how your views are displayed - can go in CFCs/functions, but sometimes makes more sense in custom tags/whatever - depends what it's doing. – Peter Boughton Aug 18 '12 at 20:50
  • 2
    I think CF9 or CF10 has an admin setting for gzip, but couldn't find anything about it in a quick search. (I know [Railo](http://getrailo.org) has such a setting.) – Peter Boughton Aug 18 '12 at 20:50
  • Thanks for lot's of useful information. I still have to do some research and see what fits me best. – Eleeist Aug 18 '12 at 20:53
  • http://cfbestpractices.blogspot.ca/2012/03/understand-what-generates-whitespace.html?m=1 – Shawn Holmes Aug 19 '12 at 00:06
5

If you have access to the CF Administrator, there is an option to suppress white space.

It is under 'Server Settings' --> 'Settings' its called 'Enable Whitespace Management'.

Scott Stroz
  • 7,510
  • 2
  • 21
  • 25
4

Try <cfprocessingdirective suppressWhiteSpace="true">

Reference: http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-76de.html

tptcat
  • 3,894
  • 2
  • 32
  • 51
  • Works but... Is there a way to make it global and not having to put it in every file? Maybe something in Application.cfc? – Eleeist Aug 18 '12 at 20:18
  • 1
    Also investigate and If you want more selective control over output but tptcat's answer addresses whitespace as a whole. – BKK Aug 18 '12 at 20:19
  • Check out Ben Nadel's great Application.cfc guide here: http://www.bennadel.com/blog/726-ColdFusion-Application-cfc-Tutorial-And-Application-cfc-Reference.htm If you want to wrap the `cfprocessingdirective` around every call you should be able to in OnRequest. Also, as @BenKoshy said, you can get finer control over this with the two tags he mentioned. – tptcat Aug 18 '12 at 20:20