« Digital Millenium Bill of Rights | Main | Using MS-SQL's NOLOCK for faster queries »

Doh! Using this.mappings in Application.cfc

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 (7)

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.

@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?

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.

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"

@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.

@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.

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.

Post a comment


Type the characters you see in the picture above.