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.

Regular Expressions Gotcha

I was playing with some code the other day and ran into a 'gotcha' that I should have seen. I knew it in a different situation and just didn't apply it to what I was doing at the time.

Basically, ColdFusion can store a Regular Expression in a variable and then use that variable within a REFind/REReplace function as if the expression was written there. In other words, ColdFusion evaluates the variable, sees that it is in the Regular Expression position of the function and uses it as the expression to parse.

If this is so well known, then what was the problem? I was doing a search for a piece of text and then use a REReplace function to replace the text I found with a different piece of text. Straightforward and all but the text that I found had a parenthesis in it (an open and a close to be exact). This means that it was a valid Regular Expression and was to be parsed. All I wanted to do was use it as the replace text, but ColdFusion wanted to do something else with it.

Bottom line is that after beating my head against the wall looking at the data I ran a few tests and then a few more and saw the issue. Makes me feel really foolish when I can't apply a known gotcha to an unknown problem. :(

Here's the code to showcase the issue:

<cfset String="this is trim(name) which is a variable inside a function">
<cfset Searchtext="trim(name)">
<cfset Position=REFind(Searchtext, String)>
<cfset String=REReplace(String, Searchtext, '<b>\1</b>')>
<cfoutput>#Position#-#String#</cfoutput>
The position returned will be 0 and there will be no replace as the text that is looked for is not trim(name) but trimname. The parenthesis in Regular Expressions are special characters to allow grouping.

Because this was a REReplace and the text was coming from a search, I didn't see the text, didn't realize that it was a valid Regular Expression and didn't realize that it would fail (Variable stored Regular Expressions are almost always used in REFind examples). It's times like this that I could use a really good debugger to see what's happening in each variable as it changes. Luckily, that debugger should be out in a few weeks.

FusionDebug

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