This is quite possibly the neatest trick ever invented for Coldfusion - so hang on to your Cf_hat. I had nearly forgotten about it until someone on CF-Talk mentioned a problem they were having. It seems they were struggling with a CMS system where users were entering hyperlinks that were incorrectly formatted (they lacked URL encoding). The dilemma was how to fix it without requiring action from the user.
The choices were slim and none of them seamless.
Here's the scoop. You probably already know that you can use CFIMPORT to create a library of custom tags (JSP or CF). You do this by designating a folder containing all the tags and a prefix which subsequently becomes a part of your call to the tag. For example, if I had a directory called "foo" with a custom tag called "bar.cfm" in it, I could do the following:
So far so good. We actually end up with a tried and true JSP type syntax. But what happens if we forget the prefix? Can we still call "bar.cfm"?
As it turns out, this works! Now take a moment and let that sink in. How can we use this new found knowledge to fix the problem above? Simple! We create a directory called "htm" (or whatever) and place a tag called "a.cfm" in it. Then we import "htm" without a prefix. When the scripting engine comes across <a it's going to call "A.cfm". All the attributes that exist in the anchor tag are going to be passed to the custom tag in the "attribute" scope. So the following code:
...now outputs the following link....
In my test I did something else. I checked for the existence of a "style" attribute. If I didn't find one, I set the style to font-size: 14pt. Here's the sample tag.
If you want to test it, simply create a directory called "htm" in the same folder as your test script and run this code:
Obviously you could use this technique to great effect. You could enforce styles and classes. You could create a click-handler that all URLs would be forwarded through. You could rewrite boldface, italics, font tags - virtually any tag with attribute type syntax. Obviously some tags would be problematic (like the body tag). Before you go hog-wild you should consider whether this technique is actually necessary. I suspect in many or most cases it is not. Don't use it just because it's neat. It definitely results in something of a management problem with your code. Now, when you look at a page, you can no longer separate out what belongs to CF and what doesn't. That makes it difficult to maintain code using this technique. Still, it might save the day in a few cases.
Muse's Old Blog (Oct. 2002)
Charlie Arehart's "hidden gems" article (Aug. 2002)
For those of trusted origin, you use regular <a href="http://someplace.com">.
For those of untrusted origin, you use <cfhtml:a href="#output#">.
Not using a prefix on import is a pretty scary thing.
-Purr
Cool stuff!
Pretty cool!
John
Of course I could throw another couple of downsides into the pot: Firstly, the next updater for Coldfusion might remove the ability to get away without a prefix. And then suddenly you have a WHOLE lot of maintenance to do in order to enjoy new Adobe fruit.
Secondly, since its being used almost like a rendering technology, relying on this technique doesn't fix your datastore and leaves other sorts of data clients (flex apps, webservices, another CF instance, etc) with the same issue. Better to clean data on the way in than persist bad data.
And like you said the support issues would be insane. I can imagine much head-scratching and ultimately head-thumping going on by the poor support developer.
Is there a way to use cfimport and then disable the library later on the page?
Use a custom tag instead of "faking" the ColdFusion interpreter into substituting a custom tag for an hmtl tag.
But then I thought of a problem we have at work. We've styled all our <cfinput>s to look like hyperlinks. The only problem is that when we do a <cfdocument format="pdf"> command it ignores CSS.
So let's say we had <cfif> that says something like this:
<cfif this is a pdf form creation>
<cfimport taglib="htm" prefix=""/>
</cfif>
Then you could change all your <input>s to be hyperlinks!
To redefine what <cfinput> means depending upon the context.
I like it.
Unfortunately this won't work. You cannot overwrite a CF tag. Remember that this technique works in conjuction with CF rendering - not before or after the fact. Consequently, "CFINPUT" is already a tag that CF knows about. You could rewrite the "input" tag in this way - but not "cfinput".
-Mark
http://www.markdrew.co.uk/blog/index.cfm/2007/4/11...
Example:
<!--- test_tags.cfm --->
<cfimport taglib="cftags" />
<test />
<!--- test.cfm --->
test data
--------------------
So test_tags.cfm imports test.cfm but instead of just seeing "test data" I see "test data test data". I can't for the life of me figure out why...please help!
<test />
is the same as
<test></test>
Note Ray's example...
<cfif thistag.executionmode IS 'End'>
<cfoutput></a></cfoutput>
</cfif>
modes are start and end.