The amazing adventures of Doug Hughes

As most people who use Mach-II knows, it inserts a lot of white space at the top of your website. This can be significant! I was getting several thousand characters of white space at the top of some of my applications and decided that was a bad thing. So, I took action against it.

The first thing I did was go though all of my components and double check that all of them have output=”false” in all methods. Some didn’t, I fixed that. I also added output=”no” into the cfcomponent tag itself. I also did a few other things to remove the vast quantities of white space. Even after all of my efforts I was still getting between 4000 and 7000 characters of white space in front of all my pages.

Finally, just to see what would happen, I wrapped the call to the Mach-II framework in my index.cfm in a cfsavecontent. I then simply output the trimmed value of that content like this:

<cfsavecontent variable="content">
    <cfinclude template="/MachII/mach-ii.cfm" />
</cfsavecontent>
<cfoutput>#trim(content)#</cfoutput>

Viola! Much smaller files and almost no leading white space. How happy I am.

Comments on: "Trimming the Whitespace From Mach-II Generated HTML" (11)

  1. Sean Corfield said:

    Seems like this change alone would trim all the whitespace without core file changes?

    Definitely a good hint…

    Like

  2. Phil Cruz said:

    Another trick is to put &lt;cfcontent reset=&quot;true&quot; /&gt; at the top of your final display view. (http://mach-ii.info/index.cfm?event=faq.viewqa&faqid=20)

    Like

  3. Phil Cruz said:

    That should be &lt;cfcontent reset=&quot;true&quot; /&gt;

    Like

  4. Doug Hughes said:

    Phil, that’s a much better suggestion. Thanks!

    Like

  5. I use this solution: I set the cfoutputonly cf setting to true first thing in my index file and wrap all output in cfoutput tags. I find this solution to be the best for minimising whitespace output because it gives you absolute control over what is output to the browser. This is especially important when your application also outputs XML.

    Like

  6. Thomas Burleson said:

    Phil noted that you can use &lt;cfcontent reset=&quot;true&quot;/&gt; at the top of your final view… but in a model-view-controller framework (such as mach-ii) you will do processing before the view is invoked. And if you set cookies, I believe that the ‘reset’ will clear the cookies and all content/headers previously assigned during the current request.

    In other words, ‘cfcontent reset’ is not cookie friendly.

    Like

  7. Laurence Middleton said:

    Thomas: I just tried the cfcontent trick in the outermost view of a Mach-ii app I’m developing, and it appears to leave cookies and session tracking alone.

    My cookies are set simply by a cfapplication tag in application.cfm at webroot with clientmanagement=&quot;yes&quot; and sessionmanagement=&quot;yes&quot;.

    Do you have an example of cookie-unfriendly behaviour in Mach-ii?

    Like

  8. How about:

    &lt;cfscript&gt;
    function HtmlCompressFormat(sInput)
    {
    var level = 2;
    if( arrayLen( arguments ) GTE 2 AND isNumeric(arguments[2]))
    {
    level = arguments[2];
    }
    // just take off the useless stuff
    sInput = trim(sInput);
    switch(level)
    {
    case &quot;3&quot;:
    {
    // extra compression can screw up a few little pieces of HTML, doh
    sInput = reReplace( sInput, &quot;[[:space:]]{2,}&quot;, &quot; &quot;, &quot;all&quot; );
    sInput = replace( sInput, &quot;&gt; &lt;&quot;, &quot;&gt;&lt;&quot;, &quot;all&quot; );
    sInput = reReplace( sInput, &quot;&lt;!–[^&gt;]+&gt;&quot;, &quot;&quot;, &quot;all&quot; );
    break;
    }
    case &quot;2&quot;:
    {
    sInput = reReplace( sInput, &quot;[[:space:]]{2,}&quot;, chr( 13 ), &quot;all&quot; );
    break;
    }
    case &quot;1&quot;:
    {
    // only compresses after a line break
    sInput = reReplace( sInput, &quot;(&quot; & chr( 10 ) & &quot;|&quot; & chr( 13 ) & &quot;)+[[:space:]]{2,}&quot;, chr( 13 ), &quot;all&quot; );
    break;
    }
    }
    return sInput;
    }
    &lt;/cfscript&gt;

    &lt;cfsavecontent variable=&quot;content&quot;&gt;&lt;cfinclude template=&quot;mach-ii.cfm&quot; /&gt;&lt;/cfsavecontent&gt;
    &lt;cfoutput&gt;#HtmlCompressFormat(content)#&lt;/cfoutput&gt;

    Like

  9. Doug Hughes said:

    Honestly, these days I just use Model-Glue which doesn’t generate whitespace.

    Like

  10. Doug Hughes said:

    Sami, Nope. Not before now. Looks like a great solution though, so long as you don’t have a need to output preformatted text files, etc.

    Like

Comments are closed.

Tag Cloud