Remove whitespace in output HTML code

2019-02-27 06:11发布

问题:

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?

回答1:

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.)



回答2:

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'.



回答3:

Try <cfprocessingdirective suppressWhiteSpace="true">

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