Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at CFUNITED 2009 (Lansdowne, VA) with:

Using CFContent's Variable Attribute Stops All Further Processing

By Ben Nadel on
Tags: ColdFusion

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.




Reader 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.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.