I just want to start off by saying that this idea was not mine. I have to give complete credit to Jeff Sanders who contacted me with this suggestion. I happen to think it's a really neat idea.
This idea has to do with the problem of creating ColdFusion components in parallel or parent directories without the use of mapped paths. A while back, I discussed getting around this idea by including a ColdFusion function into the Application.cfc/cfm file. This would create a mixin that leveraged the fact that ColdFusion component paths are relative the executing template, not the compiled page.
This worked, but the fact that it used a mixin always seemed a little less than elegant. Jeff Sanders came up with the idea to take this concept and use a ColdFusion custom tag, rather than a mixin, that could be used from anywhere in the application. To explain, let's use this sample directory structure:
In this structure, our ColdFusion components are going to be stored in the CFC folder; our website is going to be stored in the www folder. In this kind of a scenario, it would ordinarily be impossible to instantiate a component in the CFC directory from a file within the www directory without a mapped path or a mixin. To create a more elegant and portable solution, we can use a ColdFusion custom tag that lives in the root directory:
The CreateCFC.cfm ColdFusion custom tag is extremely simple and acts purely as a proxy to ColdFusion's CreateObject() method:
<!--- Kill extra output. ---> <cfsilent> <!--- Param tag attributes. ---> <cfparam name="ATTRIBUTES.Component" type="string" /> <cfparam name="ATTRIBUTES.ReturnVariable" type="variablename" /> <!--- Create the ColdFusion component and store it into the requested caller variable. ---> <cfset "CALLER.#ATTRIBUTES.ReturnVariable#" = CreateObject( "component", ATTRIBUTES.Component ) /> <!--- Exit out of tag. ---> <cfexit method="exittag" /> </cfsilent>
As you can see, all it takes is the root-relative path to the target component and the return variable into which the instantiated CFC will be stored.
This tag is meant to be invoked using a CFModule tag in which you can supply the path to the target template:
<!--- Create CFC in parallel directory using ColdFusion custom tag module. ---> <cfmodule template="../createcfc.cfm" component="cfc.Test" returnvariable="REQUEST.Test" /> <!--- Output the Test component message. ---> <cfoutput> #REQUEST.Test.Message# </cfoutput>
In a way, this ColdFusion custom tag works much like the front controllers in FuseBox or Model-Glue; all CFC creation is funnelled through this one template. Now, I am not sure you would want to use this all over the place. What you might want to do is create some sort of globally accessible user defined function that creates a kind of second layer proxy to this tag; otherwise, you would have to supply the custom tag template path every time you invoke a component.
Regardless, I thought this was a very cool idea that Jeff came up with.
Want to use code from this post? Check out the license.