I've been working with ColdFusion ever since version 4.0, but of course every once in a light bulb goes on over my head and I realize how I've completely misunderstood something. Like this.mappings in Application.cfc.
I have to admit that I had stopped trying to use this.mappings because I could never get it to work. I would create a mapping in the fashion that the docs told me to:
<cfset this.mappings['myMapping'] = "/path/to/my/folder" />
And then I'd try to refer to my mapping name in my code, as I figured I should:
<cfinclude template="/myMapping/myFile.cfm" />
But it never worked. The server would just throw an error and tell me that the mapping didn't exist.
But tonight it dawned on me, and I wanted to share the epiphany so that you can either solve the mystery, too (if you also could never figure it out) or have a good chuckle at my expense (if you can't fathom why I ever got something so simple wrong in the first place). You don't refer to the mapping name in your file path... you refer to the this.mappings variable. Doh.
So the correct use is:
<cfinclude template="/#this.mappings['myMapping']#/myFile.cfm" />
Don't laugh too hard at me. I can only hope this revelation of my own foolishness will lift the wool from someone else's eyes.

Comments (12)
March 10, 2008
10:40PM | #
Uh, I'm confused. I use this all the time and it works for me except I'm doing this instead:
And, I use
You have to use the same '/' notation just like you were setting up a mapping using the CF Administrator.
March 10, 2008
11:01PM | #
@Todd: I'm puzzled to hear that it works for you-- I've had all sorts of problems with it. I'm running it on CentOS. Have you had success with it on Linux?
March 11, 2008
12:47AM | #
You realize that mappings are FILE system mappings, and as such need expanded directory paths? On windows, for example, you might have:
this.mappings.mymapping='c:\inetpub\wwwroot\'
So then an include on "/mymapping/about/me.cfm" would work correctly. All the pound substitution is substituting the this.mappings.mymapping variable into the template string just as it would any other variable. It's not using mappings at all.
March 11, 2008
2:22AM | #
Your mistake is omitting the leading / on the mapping:
cfset this.mappings['/myMapping'] = "/path/to/my/folder"
The struct keys must be actual mappings, i.e., begin with /
cfset this.mappings["/org/cfcunit"] = "/my/thirdparty/cfcunit/org/cfcunit"
March 11, 2008
6:20AM | #
@Tom: Yes, I run CentOS on web-rat.com. I have this running just fine. Sean confirmed what I was just saying and Michael Long confirmed another thing you should check.
March 11, 2008
9:36AM | #
@Sean: I was under the impression that a leading slash shouldn't be necessary in the mapping. That being said, the addition of a slash does make the mapping work in my previous example.
June 18, 2008
9:59PM | #
This was, at least for me, a useful post because it highlighted my own ignorance about CFC custom mappings.
Yes; mappings are File system Paths, therefore an OS slash prefix is required. But it still seems a little odd to me that a programmatic path reference itself needs to incorporate a Slash. I assume it's due to the way ColdFusion references the mapping. If CFusion referenced it the same way that Tom mentioned (or provided slightly better docs about it) there might be less confusion around this topic.
June 1, 2009
7:09PM | #
Yeah... this is buggy and doesn't work. I have just verified on both a linux and a windows cf8 server installation that it does not work.
First the exact path method:
cfset this.mappings["/cools"] = getDirectoryFromPath( ExpandPath( "../cools/" ) )
Error.
Next the relative path method:
cfset this.mappings["/cools"] = "../cools"
Error.
I have tried it with and without forward slashes, with and without the exact file system path, and many other ways besides. I am convinced that it is a bug in coldfusion. Even if it isn't a bug, then the documentation that describes how to create these mappings is just plain wrong. Still no way to use mappings without either globally creating one, or using variables similar to this post. :(
December 6, 2009
9:41AM | #
@Cory: code below works just fine running cf8 on debian
December 6, 2009
9:45AM | #
cfcomponent output="false"
cfset this.mappings = StructNew()
cfset this.mappings["/myMapping"] = GetDirectoryFromPath(ExpandPath(".")) & "/myMapping"
/cfcomponent
March 8, 2010
11:49PM | #
I have been unable to get any of this to work for the purposes of extending an application.cfc into sub dir/apps. Seems I MUST manually add the mapping in CFadmin.
September 28, 2010
4:07PM | #
One very important note. You can't use the mappings you create in Application.cfc *within* application.cfc itself (or any of its includes).