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)
Seems like this change alone would trim all the whitespace without core file changes?
Definitely a good hint…
LikeLike
Another trick is to put <cfcontent reset="true" /> at the top of your final display view. (http://mach-ii.info/index.cfm?event=faq.viewqa&faqid=20)
LikeLike
That should be <cfcontent reset="true" />
LikeLike
Phil, that’s a much better suggestion. Thanks!
LikeLike
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.
LikeLike
Phil noted that you can use <cfcontent reset="true"/> 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.
LikeLike
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="yes" and sessionmanagement="yes".
Do you have an example of cookie-unfriendly behaviour in Mach-ii?
LikeLike
How about:
<cfscript>
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 "3":
{
// extra compression can screw up a few little pieces of HTML, doh
sInput = reReplace( sInput, "[[:space:]]{2,}", " ", "all" );
sInput = replace( sInput, "> <", "><", "all" );
sInput = reReplace( sInput, "<!–[^>]+>", "", "all" );
break;
}
case "2":
{
sInput = reReplace( sInput, "[[:space:]]{2,}", chr( 13 ), "all" );
break;
}
case "1":
{
// only compresses after a line break
sInput = reReplace( sInput, "(" & chr( 10 ) & "|" & chr( 13 ) & ")+[[:space:]]{2,}", chr( 13 ), "all" );
break;
}
}
return sInput;
}
</cfscript>
<cfsavecontent variable="content"><cfinclude template="mach-ii.cfm" /></cfsavecontent>
<cfoutput>#HtmlCompressFormat(content)#</cfoutput>
LikeLike
Honestly, these days I just use Model-Glue which doesn’t generate whitespace.
LikeLike
Doug,
Have you seen this?
http://weblogs.macromedia.com/cantrell/archives/2005/01/eliminate_coldf.cfm
Sami
LikeLike
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.
LikeLike