ColdFusion Application-Specific Mappings Work With CFInclude

Posted March 14, 2011 at 9:28 AM by Ben Nadel

Tags: ColdFusion

Ok, so I know this post is like 3 years too late; but, the other day, I got to thinking about the application-specific mappings introduced in ColdFusion 8 and I wondered if I had ever used them for CFInclude. I know that I use them for createObject() all the time; but, I don't think I had ever actually used (or even tested) application-specific mappings in the context of a CFInclude tag. As such, I figured I would throw a small demo together to ease my mind.

To test the application-specific mappings, I set up three directories in my application:

./com/
./content/
./templates/

Each of these directories simply includes an index.cfm file that outputs the phrase, "I am in the XYZ folder," in which XYZ is replaced with the actual folder name. Then, I set up an Application.cfc file that defined mappings for each of these directories.

Application.cfc

  • <cfcomponent
  • output="false"
  • hint="I define the application settings and event handlers.">
  •  
  • <!--- Define the application settings. --->
  • <cfset this.name = hash( getCurrentTemplatePath() ) />
  • <cfset this.applicationTimeout = createTimeSpan( 0, 0, 5, 0 ) />
  •  
  • <!---
  • Get the root of the application so that we can start
  • creating mappings that are relative to the current directory.
  • --->
  • <cfset this.rootDir = getDirectoryFromPath(
  • getCurrentTemplatePath()
  • ) />
  •  
  • <!---
  • Now that we have the root directory, let's set up mappings
  • for our various MVC code containers.
  •  
  • /view == templates
  • /controller == content
  • /model == com
  • --->
  • <cfset this.mappings[ "/view" ] = "#this.rootDir#templates/" />
  • <cfset this.mappings[ "/controller" ] = "#this.rootDir#content/" />
  • <cfset this.mappings[ "/model" ] = "#this.rootDir#com/" />
  •  
  •  
  • <!--- ------------------------------------------------- --->
  • <!--- ------------------------------------------------- --->
  •  
  •  
  • <cffunction
  • name="onRequest"
  • access="public"
  • returntype="void"
  • output="true"
  • hint="I execute the incoming request.">
  •  
  • <!---
  • Include the index file of each of our three code
  • containers; however, use the per-application mappings
  • rather than the actual folder names.
  • --->
  • <cfinclude template="/view/index.cfm" />
  • <cfinclude template="/controller/index.cfm" />
  • <cfinclude template="/model/index.cfm" />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  • </cfcomponent>

As you can see, based on the this.mappings collection defined in the ColdFusion application framework pseudo constructor, "model" maps to "com," "controller" maps to "content," and "view" maps to "templates." The onRequestStart() event handler then overrides the incoming request, including the index.cfm file from each of my mapped directories.

When we run the above code, we get the following output:

I am in the Templates folder.
I am in the Content folder.
I am in the Com folder.

Ok cool, mind at ease - ColdFusion 8's application-specific mappings work with CFInclude. That doesn't mean that I have a particularly great use-case for it (in the context of CFInclude); but, I'm happy to know for sure that it works.




Reader Comments

Mar 14, 2011 at 9:44 AM // reply »
49 Comments

Have you tried to change a mapping while a site was live and running to see if it creates and issue or works? I was not aware you could use dynamic variables like this and it is a good concept. Thanks for sharing.


Mar 14, 2011 at 9:47 AM // reply »
10,743 Comments

@John,

I have seen some issues with the use of "this.customTagPaths" and sites under load; but, I have never seen issues with "this.mappings". This is typically how I set my mappings these days and it seems to be fine.

Like I said, though, customTagPaths does seem to get a bit iffy - occasionally throwing a "Template not found" error.


Mar 14, 2011 at 2:48 PM // reply »
4 Comments

I've had just had a use case for it recently. I've written an os application framework which uses FW/1, WireBox and hyrule so i had to ensure there is a dynamic mapping for the used libraries neverthless where the framework folder is stored (webroot or applicationroot).


Mar 14, 2011 at 10:00 PM // reply »
33 Comments

I think I've had some trouble with cf "finding" components every once in a while using mappings. I told my users to press refresh and it worked. So I finally just hard coded the folder names and the problem went away. That's not scientific though.


May 4, 2011 at 5:02 AM // reply »
1 Comments

I tried this but at first it didn't work - it just showed links to Application.cfc and the directory structure. I added an index.cfm to the home directory and then it works but doesn't show any text from the index.cfm in the home directory.

From this I assume an index.cfm is required in the home directory as well to ensure that the Application.cfc is run but that it is then ignored.

Is this how it is supposed to work?

Richard


Dec 5, 2011 at 4:12 PM // reply »
12 Comments

Note: It appears that you can not use the mappings in the pseudo-constructor (e.g. you can not have a cfinclude in the pseudo-constructor that uses the mapping.) You must wait until after it (the pseudo-constructor) has completed execution.


Feb 23, 2012 at 2:54 AM // reply »
4 Comments

Ben! You said "I have seen some issues with the use of "this.customTagPaths" and sites under load".

Thats exactlt what I am seeing with my site right now just finished being load tested. A bunch of random missing template errors when including files via paths set in this.mappings.

Its totally intermittent and random. This is crazy that it doesn't perform under load?!

I guess I need to hard code all of these paths in the CFAMIN (ugh -ugly).

Anybody been able to get this working nicely?


Feb 29, 2012 at 5:22 AM // reply »
1 Comments

Brook, we have the same problem when using includes via per application mappings under load. Random missing template errors. We just abandoned using them.


Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 16, 2012 at 8:18 PM
Best Of ColdFusion 10 Contest Entry - HTML Email Utility
Just found this, looks good! I'm trying to run it on local, it's the 64bit version and I'm experiencing horrible lag. On average the generate.cfm processes the content change in 60-90 seconds. I've ... read »
May 16, 2012 at 6:40 PM
Maintaining Sessions Across Multiple ColdFusion CFHttp Requests
I am trying to integrate this CFHTTPsession into an application that will log into zeekrewards.com to post ads and I am not having any luck. The code works perfectly for logging into other websites, ... read »
May 16, 2012 at 2:44 PM
Creating A Sometimes-Fixed-Position Element With jQuery
Thank you, very useful technique! Worked like a charm. ... read »
May 16, 2012 at 1:58 PM
Movies As A Religious Experience
Acting can, in a way, ruin the movie-goer's experience. I used to be able to get so caught up in movies and their plots, and totally engaged. But lately, I haven't been able to as much with a lot o ... read »
May 16, 2012 at 1:52 PM
The Science Of Optimal Post-Exercise Nutrition
children of this age eat very less vegetables so u can opt for salads they will like it also carrot ,cucumber,onion and as far as pulses are concerned u can boil them ,give him along with mashed rice ... read »
May 16, 2012 at 1:34 PM
Strange ColdFusion JRUN Stack Overflow Error
Hey, Recently I updated my jrun4 using the latest updater 7 and now i am having memory issues :(:(:( any help is appreciated ... read »
May 16, 2012 at 9:56 AM
ColdFusion 10 Beta, Apache Tomcat, And Symbolic Links On Mac OSX
Hi, Now that ColdFusion 10 is out I have stumbled over this as well and I cannot figure out the proper solution. We're running virtual hosts via Apache2; the ColdFusion-applications store their fil ... read »
May 15, 2012 at 6:03 PM
Movies As A Religious Experience
@Ben, I don't know whether you'd consider this a religious observation, but it seems to me, in a sense, movies multiply how many lives we get to have. Each movie is like a little extra life we get ... read »