ColdFusion TIPS PLUS
Issue 00112 http://www.cftipsplus.com
I. My CommentsII. ColdFusion In Context: Whiteout
By R. Martin Ladner
martin.ladner@charter.net
See our sponsors at the bottom of this e-mail.
I. Comments:
Hope things are looking up with all of you. The economy seems to be making the turn towards up. I think we have hit rock bottom and are back on our way up.
Keep Coding,
Nathan Stanford
http://www.cftipsplus.com
If you have suggestions for articles send them to us.
If you would like to write for cftipsplus.com
send us an email to:
NathanS<at>nsnd.com
Books For Sale
http://nsnd.vstorelibrary.com/
IF YOU WANT TO BE AN AUTHOR SEND IN YOUR COLDFUSION TIPS.
Remember this is a great way to get your name known in the
ColdFusion Community.
II. ColdFusion in Context: Whiteout
By R. Martin Ladner
martin.ladner@charter.net
No, I'm not advocating that you purchase bottles of white liquid to hide mistakes. This tip demonstrates how to remove extra white space associated with nicely formatted code.
Problem
You don't usually run your tags together like this. It would be hard to read pages and pages full of run-on code like this:
<cfif test condition>do this<cfelseif>do that</cfif>
For clarity, you usually format your code this way:
<cfif test condition>
do this
<cfelseif>
do that
</cfif>
However, the extra spaces and linefeeds/carriage returns go to the browser and may be seen by selecting "view source". Due to compression, white space has little impact on download times. However, if you want to remove it, there is a way.
Example
Place all code in whiteout.cfm. Start with this example. It initializes variables that will be used later. It does a simple test to determine if the day is odd or even. The formatting used to make the statements clear can usually be seen.
<hr>
View source to see that the browser normally gets
HTML, text, and white space used in formatting code.
<p>
<!-- This normal comment usually appears --->
<!--- This ColdFusion comment never appears --->
WHAT KIND OF DAY IS TODAY?<br>
<cfset Counter=1>
<cfset Today=now()>
<cfif day(Today) mod 2>
<cfoutput>Today, #dateFormat(Today)#, is an odd day.</cfoutput>
<cfelse>
<cfoutput>Today, #dateFormat(Today)#, is an even day.</cfoutput>
</cfif>
<cfoutput>This is example number #Counter#</cfoutput>
<cfset Counter=Counter+1>
Enable ColdFusion Output Only
One solution is to enable only ColdFusion's output: cfoutput tags, cfform's javascript that ColdFusion generates automatically, and the writeOutput() function in cfscript. Except for a gap that seems to be introduced by the writeOutput() function, the ONLY characters passed to the browser are explicitly sent by ColdFusion.
<hr>
The cfsetting tag with enablecfoutputonly set to yes
blocks everything that ColdFusion didn't generate.
This includes the text heading "WHAT KIND...".
The output of cfoutput tags runs together seamlessly.
The output of writeOutput() in a cfscript tag isn't
quite as seamless, and the javascript ColdFusion
generated for the dummy form appears as it normally does.
<p>
<cfsetting enablecfoutputonly="yes">
<!-- This normal comment usually appears --->
<!--- This ColdFusion comment never appears --->
WHAT KIND OF DAY IS TODAY?<br>
<cfif day(Today) mod 2>
<cfoutput>Today, #dateFormat(Today)#, is an odd day.</cfoutput>
<cfelse>
<cfoutput>Today, #dateFormat(Today)#, is an even day.</cfoutput>
</cfif>
<cfoutput>This is example number #Counter#</cfoutput>
<cfscript>
writeOutput('!');
</cfscript>
<cfset Counter=Counter+1>
<cfform name="Fred">
<cfinput name="Ignored" type="text" required="yes">
</cfform>
<cfsetting enablecfoutputonly="no">
The Rest Is Silence
Nothing between cfsilent beginning and ending tags will reach the browser.
<hr>
This time, the cfsilent tag has blocked
all output, even text inside of a cfoutput tag.
The example known as number 3 does not appear.
<p>
<cfsilent>
<!-- This normal comment usually appears --->
<!--- This ColdFusion comment never appears --->
WHAT KIND OF DAY IS TODAY?<br>
<cfif day(Today) mod 2>
<cfoutput>Today, #dateFormat(Today)#, is an odd day.</cfoutput>
<cfelse>
<cfoutput>Today, #dateFormat(Today)#, is an even day.</cfoutput>
</cfif>
<cfoutput>This is example number #Counter#</cfoutput>
<cfset Counter=Counter+1>
</cfsilent>
A Little Extra
Of course, the logic used above could be streamlined as in the example below, but the impact of formatting code would not be as obvious for our purpose here. Also, the code below shows a way to suppress debug output.
Suppose you usually display debug output in your development environment but would like to suppress it for specific files. Perhaps these files are part of a frameset and it's hard to see what the overall page should look like when debug information is showing. The cfsetting tag with showdebugoutput set to "no" will take care of this. Note that setting this to "yes" will not automatically display it; settings in ColdFusion Administrator take precedence.
<hr>
Just as a reminder that the odd/even logic
could have been handled by a single function,
look here.
<p>
<cfoutput>Today, #dateFormat(Today)#, is an
#iif(evaluate(day(Today) mod 2),de('odd'),de('even'))#
day.</cfoutput>
<cfoutput>This is example number #Counter#</cfoutput>
<hr>
<cfsetting showdebugoutput="no">
Finally, notice that the debug output many of you
have configured your environment to display
does not appear. It has been blocked by the cfsetting
tag with showdebugoutput set to "no".
Review
Let's white out white space. If you have a page full of complex logic and little desired output, and the only text you need will come from ColdFusion - you don't need HTML tags, etc. - then cfsetting with enablecfoutputonly can be useful. If you don't want ANYTHING to show, cfsilent can do the job. If you want to suppress debug output, cfsetting with showdebugoutput turned off will help.
=Marty=
SPONSOR ADS:
This e-mail is sponsored by the following ads.
Books For Sale
http://nsnd.vstorelibrary.com/
Advanced, Intensive ColdFusion Training!Visit this site. If you have plans to get training here is a company
that provides Advanced, Intensive ColdFusion Training. Check them out.
http://www.coldfusiontraining.com/index.cfm?ref=cftipsplus
Publisher and Creator:
Nathan Stanford,
NathanS<at>nsnd.com
http://www.cftipsplus.com
Macromedia and ColdFusion are U.S. registered trademarks.
Copyright (c) 2000 - 2002
CFTIPSPLUS.COM and NSND.COM
Permission is granted to circulate this publication via
MANUAL forwarding by email to friends provided that the text is
forwarded in its entirety and no fee is charged.