Overwriting HTML tags using cfimport
April 11, 2007 ·
Here is a quick, ColdFusion tip about using cfimport that I learned many years ago in some ColdFusion training I did for CFMX 6, but I guess its still relevant.
You can re-implement any HTML tag using a custom tag by using cfimport. To do this you have to firstly create a directory where you are going to store your custom tags, so lets say /htmltags under your webroot. Then at the top of the page that you are going to be doing the replacement in put the following:
<cfimport prefix="" taglib="/htmltags">
<a href="somelink">I dont like how Links are done</a>
Now in your /htmltags add a file called a.cfm with the following content:
<cfparam name="attributes.href" default="">
<cfif ThisTag.executionmode IS "start">
<a href="<cfoutput>#attributes.href#</cfoutput>" style="overWrittenStyle">
</cfif>
<cfif ThisTag.executionmode IS "end">
</a>
</cfif>
And you will get the style attribute added. This is just an example, you could loop through all the attributes and output them in the tag, and do whatever you like of course.
(now back to document writing... fun fun fun)
Tags: coldfusion · webdev
11 responses
1 Terrence P Ryan // Sep 22, 2008 at 4:13 PM
Thanks for sharing this Mark.
2 Mark Drew // Sep 22, 2008 at 4:13 PM
3 Phillip Senn // Sep 22, 2008 at 4:13 PM
I was trying to use cfdocument, but the submit buttons looked ugly in the generated pdf file.
My cfinput's are styled, but they lose their styling with the cfdocument command.
Hal Helms talks about the same phenom with Jeff Peters in one of their episodes.
20 minutes into their Downtime podcast, they talk about style sheets with cfdocument.
I've never gotten around to actually doing it - it's just something I'm keeping on my radar.
Perhaps a print CSS would be better rather than creating a pdf file.
4 Phillip Senn // Sep 22, 2008 at 4:13 PM
http://mkruger.cfwebtools.com/index.cfm/2006/5/10/adaptive.tags
5 Dan Sorensen // Sep 22, 2008 at 4:13 PM
Replace your executionmode IS start with the following:
<cfset linkStyle = "normalLink">
<cfif FindNoCase("http://", attributes.href) AND NOT FindNoCase(cgi.SERVER_NAME, attributes.href)>
<cfset linkStyle = "offsiteLink">
</cfif>
<cfoutput>
<a href="#attributes.href#" class="#linkStyle#">
</cfoutput>
</cfif>
Then add the following CSS to your stylesheet:
a.normalLink {/*stying for internal links here */}
a.offsiteLink {padding-right: 12px; background: url(/icons/offsite.gif) no-repeat right;}
Note: You'll need to find a little offsite icon image to reference with your CSS. You can see an example of my icon at my page here: http://www.nwserviceacademy.net/apply.cfm
Thank for the good idea. :-)
6 Mark Drew // Sep 22, 2008 at 4:13 PM
@Dan: that is a great little hint, I think we should put this into blog CFC, of course, I don't thing it would work if you are outputting from a query. You would have to render the content down and add the import, which is a bit annoying.
7 Marcos Placona // Sep 22, 2008 at 4:13 PM
Nice tip though!
Cheers
8 Phillip Senn // Sep 22, 2008 at 4:13 PM
I wasn't trying to say "This is old news".
I was trying to encourage anyone interested in developing this idea further.
9 Mark Drew // Sep 22, 2008 at 4:13 PM
Still thinking of more ways to (ab)use this too...
10 Hive // May 27, 2010 at 9:31 PM
11 pphfnhbha // Jul 25, 2011 at 11:05 AM