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 cf.Objective() 2014 (Bloomington, MN) with: Jeff McDowell and Joel Hill and Jonathan Rowny and Shawn Grigson and Jonathan Dowdle and Matt Vickers and Christian Ready and Asher Snyder and Clark Valberg and Oscar Arevalo and David Bainbridge

GetPageContext() Is Template Specific, Not Request Specific

By Ben Nadel on
Tags: ColdFusion

This morning, I was fooling around with some ColdFusion GetPageContext() stuff when I happened upon a little interesting fact: GetPageContext() is template specific. Now, I know very little about Java and what the actual page requests consist of, but I had just always thought that there was one GetPageContext() object per page request. But, as it turns out, there is a different "context" object per ColdFusion template.

To demonstrate this, I am merely getting the HashCode() of the GetPageContext() on two different templates. On each of the templates, I call the HashCode() twice to demonstrate that the multiple calls on the same template have the same outcome and that calls across templates have different outcomes.

Here is the main template:

  • <cfoutput>
  •  
  • <p>
  • Main Template:
  • </p>
  •  
  • <p>
  • #GetPageContext().HashCode()#<br />
  • #GetPageContext().HashCode()#<br />
  • </p>
  •  
  • <cfinclude template="include.cfm" />
  •  
  • </cfoutput>

And, here is the included template:

  • <cfoutput>
  •  
  • <p>
  • Sub Template:
  • </p>
  •  
  • <p>
  • #GetPageContext().HashCode()#<br />
  • #GetPageContext().HashCode()#<br />
  • </p>
  •  
  • </cfoutput>

Running the above main template, I get the following output:

Main Template:

5790031
5790031

Sub Template:

5205615
5205615

Notice that the GetPageContext() is returning a different object for each of the two different templates. Very interesting. I am sure some of you smarter fallas out there are gonna look at this and think, "D'uh, what did you think it was gonna do?" Yeah, well I am barely able to wrap my head around the massive page context object :) There is so much to here to dig through.




Reader Comments

@George,

Multiple versions of the return Page Context might reference the same request object, but how can they be page specific AND return different HashCodes on different templates of the same request? Maybe I am not understanding what a hash code represents?

Reply to this Comment

Ben,

There's a difference between a PageContext and Request. A Request corresponds to an HTTP Request that CF (really, it's underlying JEE server) has been sent. There is a corresponding Response object that contains headers, the outputted content, etc. The PageContext object, on the other hand, is specific to the given page (CFM template) that is currently executing. This is exactly the same paradigm as JSP uses.

So if a request requires exactly one CFM template, there will be one Request, one Response, and one PageContext that wraps them. If it requires five CFM templates, there willl still be on Request, one Response, but five PageContext objects. This gets a bit muddied when you get into CFC land, as you might expect, but the basic idea holds.

Reply to this Comment

I wasn't particularly clear :s

GetPageContext() returns an object that gives the page the tools it needs to do its job... things like the request, response, scopes, session, etc. Each template will have its own page context as it has its own scopes, attributes, etc, so you get the same object returned twice if you call getPageContext() twice on the same template, and why you'll get different contexts for each template (included or otherwise).

Hope this helps. I nearly came up with a brilliant analogy involving making cups of tea but couldn't make it work for the included template :(

Reply to this Comment

I like tea :)

That sounds good guys, thanks for the feedback. I think that gives me a better place form which to do further exploration.

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.