Sam Farmer

Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened.

But I have become passionate about making software.

How in one line to extend ColdFusion by writing CFC's

January 29, 2010

There have always been ways to extend ColdFusion through custom tags (and its variations like cfmodule) or by adding a library cfc or by including a file before every page.

I now present another way;

And its all done by one setting:

this.customtagpaths = pathToExtensionFolder!

Lets see it in action.  First, set up an Application.cfc like so:

component
{
     this.customtagpaths = getDirectoryFromPath( GetCurrentTemplatePath() ) & "languageExtension/";
}

Then in the langaugeExtension folder add as many cfc's as you want for my example I included one called Invoke.cfc (yes, I've wanted a script based version of this for a while! ;) ):

component {
function first() {
     return "Sam";
}
}

In the root folder I created a test.cfm with the following code:

<cfscript>
a = new languageExtension.Invoke();
writeDump(a);
 
i = new Invoke();
writeDump(i);
o=i.first();
writeOutput(o);
</cfscript>

Being in the root folder the first line should always work.  Nothing exciting there.  But whats cool is line 4 where variable i is set with no path to Invoke().  And it works.  Here is a screenshot:

Next I created a sub folder structure to see if this would still work further down the chain.

Folder Set Up

And here is the code for dirB/test.cfm:

<cfscript>
i = new Invoke();
writeDump(i);
o=i.first();
writeOutput(o);
</cfscript>

Which works as well!  Here is its screenshot:

Dump of dirB/test.cfm

You can also place extra CFCs in the servers root CustomTags folder and use new on them.  I think this is a bad idea.  Its better to write applications that can be dropped anywhere and are not dependent on special server set ups.

So a pretty easy way to more natively expand upon the ColdFusion language and while I mention this.customtagpaths above it is also possible thanks to the new operator introduced in 9.


No Comments

Some comments have been lost over the years due to moving hosts.

More


More blog entries that I have written.

Code coloring by PRISM.