Ask a Jedi: CFIMPORT, Application.cfc, and custom tags

This post is more than 2 years old.

Tony asks two interestin questions:

1. I really like cfimport for being able to call my customtag libraries by directory vs by file and then using prefix:tagname. However, I do NOT like having to add that line of code to every page. I think I remember hearing/reading that cfimport cannot be used in Application.cfc. Is this correct? I tried adding a cfimport call into onRequestStart but it didn't work. It didn't throw an error, it just didn't render my masterlayout elements. Thoughts?? (NOTE: I have no problem using cfmodule or's what I've been using as of late. It's really the concept of organizing my customtags in directories that had me considering cfimport instead.)

So yes, if you do use cfimport, you must include it on every page. However, you actually can use cfimport on an Application.cfc page, it just won't "carry over" into the current CFM file. I added a cfimport to a sample Application.cfc file, inside my cfcomponent tag, and inside onRequestStart, I did:

<cffunction name="onRequestStart" returnType="boolean" output="true"> <cfargument name="thePage" type="string" required="true"> <cfoutput>header call: </cfoutput><ray:time><cfoutput> end header call</cfoutput> <cfreturn true> </cffunction>

"ray" was the prefix I had used, and time just outputs now(). This works fine, although please note I don't support outputting headers in onRequestStart. But if you wanted to use cfimport inside Application.cfc itself, you can do it.

2. If I end up just sticking with good ole cfmodule or cf_tagname, is there a way to specify customtag paths and mappings in Application.cfc (vs opening up CF Admin to do it)?

In ColdFusion 8, this is baked in. You can create both mappings and custom tags using the This scope in the Application.cfc file. You can read more about this feature in LiveDocs: Specifying settings per application (PS to the Live Docs team - why do you make it so hard to find the link to a page? In the old Live Docs, I remember a URL at the bottom of each page. This seems to have been removed.)

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Nathan Mische posted on 1/27/2008 at 2:51 AM

One thing to keep in mind, you can't use application specific mappings with cfimport.

Comment 2 by John Farrar posted on 1/27/2008 at 9:05 AM

I have proposed for the "next" CF that we add Application.cfc based tag library prefix settings. The issue as it has been explained to me is that with each compile of the CF script page into java code it must create these mappings. Therefore to have a dynamic variable would create a performance hit that would not be acceptable. Yet, it does seem that to hardcode these inside the Application.cfc would be something that would resolve this. Yes, every time these got updated that would mean any files with that prefix would have to be reconverted. Yet, the programming gain seems worth it.

Comment 3 by Geoff Bowers posted on 1/27/2008 at 11:53 AM

Application specific mappings are broken if you ask me:

There is no reason why you couldn't have a compile time config for the application. Hell if New Atlanta (Bluedragon) can do it, there's little or no reason Adobe can't. In any event the documentation needs to be updated -- as it stands it implies Application specific mappings are like their CFIDE equivalent. They are not.

Comment 4 by Raymond Camden posted on 1/28/2008 at 1:29 AM

I'm not sure I'd call it broken. I can see your points about what it doesn't support, but it is still darn handy in my opinion.

Comment 5 by Tony Piscotti posted on 1/28/2008 at 9:18 PM

I personally wouldn't prefer calling site headers directly from Application.cfc either. I was just hoping there might be a way to organize the cfimport calls into one area to keep it simple and cut down on redundancy. Similar to doing a cfinclude of UDF libraries in onRequestStart, for example.

But I'm definitely fine with using cfmodule or cf_tagname formats for now.

I appreciate the assistance on this one...thank you!


Comment 6 by Joshua Curtiss posted on 2/14/2008 at 12:00 PM

Excuse me if I'm missing the mark here, but don't forget that you CAN group custom tags with directories within your custom tags directory. That grouping will not be reflected in the custom tag name, but your physical files will be organized.

Comment 7 by Tony Piscotti posted on 2/14/2008 at 8:20 PM

So, basically, you're saying I can for have the following type of directory structure:


and as long as my mapping is set for that top-level /customtag directory of that site it will also account for any tags within the subdirectories?

Just wanted to make sure I understand what you're saying.

Thanks, Joshua!