ColdFusion 8 Per App Settings - Mappings

According to the documentation for this feature, Per App mappings allows you to dynamically set "logical aliases for paths to directories on your server." This sounds great but the code example in the documentation is wrong and this leads to a lot of frustration.

The first problem is that the docs show how to set a mapping for an application but not how to use it. We could experiment a bit and eventually find out the exact syntax needed except for the second problem - the example code given to set the mapping is wrong.

The documentation says to use the following to set a mapping of "MyMap" pointing to the location "c:\inetpub\myStuff".

<cfset this.mappings["MyMap"]="c:\inetpub\myStuff">
If we place a file called test.cfm into the "c:\inetpub\myStuff" directory we should be able to include it using standard cfinclude syntax. The problem is, no matter how we try, we can not get this to work.
<cfinclude template="MyMap/test.cfm">
<cfinclude template="/MyMap/test.cfm">
<cfinclude template="#MyMap#/test.cfm">
<cfinclude template="/#MyMap#/test.cfm">
<cfinclude template="this.mappings.MyMap/test.cfm">
<cfinclude template="/this.mappings.MyMap/test.cfm">
<cfinclude template="#this.mappings.MyMap#/test.cfm">
<cfinclude template="/#this.mappings.MyMap#/test.cfm">
<cfinclude template="this.mappings['MyMap']/test.cfm">
<cfinclude template="/this.mappings['MyMap']/test.cfm">
<cfinclude template="#this.mappings['MyMap']#/test.cfm">
<cfinclude template="/#this.mappings['MyMap']#/test.cfm">
Every one of the attempts above results in an error. This leads to a ton of frustration and a search across the net for working examples. The problem with that is that there are no clear cut examples showing how to make this work.

The solution lay partially in a blog post by Sean Corfield (Scazu Powered By ColdFusion 8) where he shows how he created a number of dynamic mappings for his application. In his example code he added a forward slash before the name of the mapping, something not mentioned in the ColdFusion documentation. This is the key. This single character makes the difference between frustrating failure and a successful feature. Adding the slash we get an setting of:

<cfset this.mappings["/MyMap"]="c:\inetpub\myStuff">
and a usage of
<cfinclude template="/MyMap/test.cfm">
Frustration solved.

So to recap, ColdFusion 8 allows an application to have dynamic mappings. These are defined in the application.cfc using a syntax of this.mappings["/MyMap"] where MyMap is the mapping name. Once set, the dynamic mapping is used like any other mapping with a syntax of "/MyMap/...".

One fast note - We can use any name we want for a dynamic mapping even if the name is in use by a mapping set in the administrator. The only restriction is that if we try to set a dynamic mapping with "/", an error will be thrown. We always have to have some text after the forward slash.

Comments
Raymond Camden's Gravatar Be sure you log a bug with Adobe. For me - I'm just used to knowing that all mappings are /X or /X/Y etc. But the docs should be more clear on the matter.
# Posted By Raymond Camden | 3/25/08 7:35 AM
Ben Nadel's Gravatar This was super frustrating for me when I was learning ColdFusion 8! The documentation is not helpful at all. I am glad you put this out there.
# Posted By Ben Nadel | 3/25/08 9:24 AM
Jason Dean's Gravatar I was just dealing with this earlier this week. Thanks for the post, I can revisit it now and see if it works right.
# Posted By Jason Dean | 3/25/08 9:47 PM
Ahamad's Gravatar Well, the forward slash is missing... we need to modify it on the livedocs. Anyways, I have posted an example here...
http://coldfusion8.blogspot.com/2008/03/per-app-se...

Ahamad,
ADOBE CF Team.
# Posted By Ahamad | 3/26/08 11:41 AM
Julian Halliwell's Gravatar That's odd. I reported this as a "bug" in the livedocs in August of last year and it was fairly quickly corrected (see comments on Ben Forta's blog post where he also gave a misleading example http://www.forta.com/blog/index.cfm/2007/4/24/Scor... ).

Now the livedocs page seems to have "lost" the correction and comments.

http://livedocs.adobe.com/coldfusion/8/htmldocs/he...
# Posted By Julian Halliwell | 3/27/08 4:59 AM
Ahamad's Gravatar Hmm... we need check why this is happening in the livedocs.
# Posted By Ahamad | 3/31/08 7:42 AM
Julian Halliwell's Gravatar I reported the issue (ie the disappearance of the correction) to Adobe on 27 March.
# Posted By Julian Halliwell | 3/31/08 7:50 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9. Contact Blog Owner
House of Fusion | ColdFusion Jobs @ House of Fusion | Fusion Authority