tricky scoping in Application.cfc
February 5th, 2007
So one of the most obnoxious things about ColdFusion (in my experience) has been the drudgery of
<cfinclude template="../../../header.cfm">etc etc. Really, not any fun at all. THEN, I get ColdFusion Web Application Construction Kit (CFWACK) by Ben Forta et. al, and I read about application.cfc. This has fun stuff like onRequestStart and onRequestEnd methods, which allow you to automagically include header and footer files. Great!
Well, it wasn’t so great, because including these files was more like a static server-side include. Scope wasn’t being shared between my header, footer, and page body. Cue silly googling of “coldfusion sucks” for a momentary bitchfest. Ok, that was fun, but SURELY there has to be some way around this, right?
Thankfully, the answer was not far from hand. Googling “coldfusion scope application.cfc” (no quotes) led me to this LiveDocs page:
Er, god dammit, LiveDocs just went down. My point, though, is this:
If you don’t have an onRequest method declared in your application.cfc, then onRequestStart and onRequestEnd will NOT share their variables scope with any included templates. So as long as you have an onRequest method like this:
<cffunction name="onRequest"> <cfargument name = "targetPage" type="String" required="true"/> <cfinclude template = "#Arguments.targetPage#"> </cffunction>
Then, anything you set in variables will be shared, AS WELL as any any other templates you may have to include in your pages, such as UDF libraries, etc.
Anyway, I feel a little bit better about ColdFusion than I did when I woke up this morning.
Sorry, comments are closed for this article.