Skip to main content
Ben Nadel at Scotch On The Rock (SOTR) 2010 (Munich) with: Michael Hnat
Ben Nadel at Scotch On The Rock (SOTR) 2010 (Munich) with: Michael Hnat

Using CFContent's Variable Attribute Stops All Further Processing

By
Published in Comments (4)

Yesterday, it was called into question whether or not the use of CFContent's Variable attribute prevented any further processing of the page request. I had suggested it did; but, I wasn't 100% sure. Thinking back, I couldn't remember if I have ever had any code after a CFContent tag that used the Variable attribute (often times, it's the last thing on the page). As such, I thought it'd be easy to whip up a quick test of ColdFusion's expected functionality.

The first thing I did was enable session management for the page request. I did this because the ColdFusion livedocs don't expressly refer to page processing - they refer simply to page output:

When you use this attribute [Variable], any other output on the current CFML page is ignored; only the contents of the file are sent to the client.

This is a bit ambiguous as to whether it refers to code execution or just to page output. By enabling session management, we can test session value updates even if the generated output is not being streamed to the client.

Application.cfc

<cfcomponent
	output="false"
	hint="I define the applications settinsg and event handlers.">

	<!--- Define application settings. --->
	<cfset this.applicationTimeout = createTimeSpan( 0, 0, 5, 0 ) />
	<cfset this.sessionManagement = true />
	<cfset this.sessionTimeout = createTimeSpan( 0, 0, 0, 20 ) />

</cfcomponent>

Now that session management is enabled, I created a page that generated page output and updated session values both before and after a CFContent tag:

<!---
	Param the session hit counter to make sure that output
	is not the only thing that gets altered.
--->
<cfparam name="session.hitCount" type="numeric" default="0" />

<!---
	Create a path to the log file so that we can track the
	parts of the page that actually process.
--->
<cfset logFilePath = expandPath( "./log.htm" ) />


<!--- ---------------------------------------------------- --->
<!--- ---------------------------------------------------- --->


<!--- Increment the hit count. --->
<cfset session.hitCount++ />

<!--- Log that we made it this far in the page request. --->
<cfdump
	var="Before CFContent (#session.hitCount#)"
	output="#logFilePath#"
	/>


<!--- ---------------------------------------------------- --->
<!--- ---------------------------------------------------- --->


<!---
	Create a binary variable of a text value and stream it
	to the client using the Variable attribute.
--->
<cfcontent
	type="text/plain"
	variable="#toBinary( toBase64( 'Hey there buddy!' ) )#"
	/>


<!--- ---------------------------------------------------- --->
<!--- ---------------------------------------------------- --->


<!--- Increment the hit count. --->
<cfset session.hitCount++ />

<!--- Log that we made it this far in the page request. --->
<cfdump
	var="After CFContent (#session.hitCount#)"
	output="#logFilePath#"
	/>

As you can see, I am incrementing and logging the session's hitCount variable both before and after the CFContent tag. In this way, I can see if the session value is updated post-CFContent even if the "output" generated by the CFDump tag is not rendered. After refreshing the page a few times, here's what the log file contains:

Before CFContent (1)
***********************************************************

Before CFContent (2)
***********************************************************

Before CFContent (3)
***********************************************************

Before CFContent (4)
***********************************************************

As you can see, not only does the second CFDump tag never render to the log file, the incrementing session hitCount demonstrates that no code was executed after the CFContent tag.

I had thought that this was the expected CFContent behavior, so it's nice to see ColdFusion work this way. That said, I should remind people that this is only for a very specific use-case of CFContent - the Variable attribute (as well at the File attribute); all other uses of the CFContent tag will simply affect the response headers and the existing output buffer, allowing the rest of the page to process normally.

Want to use code from this post? Check out the license.

Reader Comments

1 Comments

Ben,

I am a lawyer that loves coding. I was in the field in the '80 prior to going to law school. Worked with CP/M machines primarily.

Anyway, I have a simple enough question about CF that I cannot find an answer for and thought "Ben would know".

I have a legal pdf form I created in LiveCycle Designer. I have a cfm form that I populate the firstname, mi and lastname. I post the form, the form populates using cfpdfform, and displays in the browser (destination field blank in cfpdfform).

All I want to do, is to give that form a name when it is displayed. It either gives the name as 123456_temp.pdf (some other numerical arrangement followed by_temp.pdf) or as cft.cfm which is the cfm code page that contains the cfpdfform code.

All I want to do is have the form displayed in Adobe Acrobat or REader with the name #form.lname#-#form.fname#.pdf

Thanks Ben.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel