Application Events: onRequest

I mentioned in a previous blog entry that I had a site (Oklahoma Medical Reserve Corps) that is XHTML 1.0 Strict and can also be served up in MS Word. As I mentioned in that entry, the two unfortunately conflict on one point:

XHTML 1.0 Strict doesn't allow an align attribute in an img tag, instead relying on CSS for positioning. MS Word, meanwhile, expects an align attribute and fails to recognize my CSS for image alignment.

I have been unable to find a way to solve the problem in CFMX 6.1 so far (If you do, please let me know). In ColdFusion MX 7, however, it would be pretty easy.

ColdFusion MX 7 comes with a new feature called Application Events which uses a new special file called "Application.cfc". If you use Application.cfc, then Application.cfm and onRequestEnd.cfm won't fire. Application.cfc allows you to capture errors, the start and (ta da!) end of a session, the start and end of an application, and the start and end of a request. It also allows you to supercede the request event (the page being called) as well.

OK. No time today to cover all of the things you can do with Application Events right now, so I will try to make this example as painfully simple as possible. Let's first replace (in the laziest way possible) our existing site structure with Application.cfc. Keep in mind, that this approach is for illustration only. I don't necessarily recommend you literally do what I am doing in practice.

Application.cfc:

<cfcomponent>
<cffunction name="onRequestStart"><cfinclude template="Application.cfm"></cffunction>

<cffunction name="onRequestEnd"><cfinclude template="OnRequestEnd.cfm"></cffunction>
</cfcomponent>

We have changed our code to use Application.cfc without changing the behavior of our site at all (keep in mind that you only need the onRequestEnd part if you site is using OnRequestEnd.cfm).

Now let's add an OnRequest event which will supercede the normal calling of the page. Note that any variables-scoped variables created in Application.cfc (or any file included by Application.cfc) will be available to the file being requested if-and-only-if it is included via the onRequest method.

<cfcomponent>

<cffunction name="onRequestStart"><cfinclude template="Application.cfm"></cffunction>

<cffunction name="onRequest">
<cfargument name="targetPage" type="string" required="true">
<cfinclude template="#arguments.targetPage#">
</cffunction>

<cffunction name="onRequestEnd"><cfinclude template="OnRequestEnd.cfm"></cffunction>
</cfcomponent>
Still no change in behavior. So far, so good. OK, now that we have changed the structure of our code, let's take advantage of that change. From now on, I am only going to look at the code in the "onRequest" event.

So, in the onRequest event, we have this:
<cfinclude template="#arguments.targetPage#">

Now let's check if this page is being served up in MS Word.
<cfif isDefined("url.format") AND url.format eq "word">
<cfinclude template="#arguments.targetPage#">
<cfelse>
<cfinclude template="#arguments.targetPage#">
</cfif>
All we have to do now is change our class="right" and class="left" to align="right" and align="left" when the document is being served in MS Word.
<cfif isDefined("url.format") AND url.format eq "word">
<cfsavecontent variable="contents"><cfinclude template="#arguments.targetPag#"></cfsavecontent>
<cfset contents = ReplaceNoCase(contents, 'class="right"', 'align="right"', 'ALL')>
<cfset contents = ReplaceNoCase(contents, 'class="left"', 'align="left"', 'ALL')>
<cfoutput>#contents#</cfoutput>
<cfelse>
<cfinclude template="#arguments.targetPag#">
</cfif>
That's it! Now, it should be noted that this code would replace all instances of class left or right with align in all tags (not just 'img') but won't get instances where more than one class is used on a single element. Perhaps a regular expression is needed, but that is a topic for another day. Nevertheless, the problem (at least in its simplest form) is solved.

That should provide a nice glimpse into Application Events. I will cover more on Application Events in future posts. Good luck!


Want to learn more about Application.cfc? Check out the Managing Requests in Application.cfc in Macromedia LiveDocs.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.