Thread: cfincludes
View Single Post
Old 12-08-2009, 01:00 AM   PM User | #31
Gjslick
Regular Coder

 
Join Date: Feb 2009
Location: NJ, USA
Posts: 476
Thanks: 2
Thanked 70 Times in 69 Posts
Gjslick will become famous soon enough
Whoa whoa whoa, #1, don't use <cfcontent> at all. If you want to know what it actually does, then read about it in the CF livedocs, but otherwise Ben Nadel (the guru that he is) is just using it really as a hack to suppress every little wee-bit of whitespace. If you were to use it in all of your cfincludes, it would actually remove the outputted content of all includes before the one that was currently processing, and thus you would only be left with the outputted content of the last cfinclude.

I really recommend just using <cfsilent>, and just wrapping it around all of your "processing" code at the top of the page.
Code:
<cfsilent>

   <!--- suppressed output code goes here (cfsets, queries, whatever).  No line breaks will be kept in the output --->
   <cfset myVar1 = "Test">
   <cfset myVar2 = "Test 2">

</cfsilent>(Line break 1)
(Line break 2)
<!DOCTYPE ...  >
<html>
blah blah blah rest of the page
That will only leave you with two linebreaks at the top of the page, unless you really wanted to be crazy and put the <!doctype> tag right next to your </cfsilent>, then that would suppress those two extra line breaks, but reduce your source code readability (which of course, is fail).

Otherwise, use the <cfsetting> tag with enableCFOutputOnly="yes" (or "true" for the attribute value, whatever you prefer).
Ex:
Code:
<cfsetting enableCFOutputOnly="yes">

Absolutely everything in the template is not added to the output buffer, 
UNLESS that text is between a <cfoutput> and a </cfoutput> tag.
This text itself is not added to the output buffer, and will not be sent to the client's browser.

<cfoutput>This text WILL be added to the output buffer, and WILL be sent to the client's browser.</cfoutput>
I actually do just that on simple pages meant for returning JSON data to an AJAX call. Here's an example directly from production code on my site. Don't mind the code that you might not understand, it's just an example of using enableCFOutputOnly.
Code:
<cfsetting enableCFOutputOnly="yes">

<cfset user = application.UserManager.getUser( form.userID )>

<!--- Return JSON object to client. --->
<cfoutput>
{
  userID: #user.getUserID()#,
  firstName: "#jsStringFormat( user.getFirstName() )#",
  lastName: "#jsStringFormat( user.getLastName() )#"
}
</cfoutput>
Only the text wrapped in the <cfoutput> tag will be returned to the client.

Hope that helps.

Greg
Gjslick is offline   Reply With Quote