Overwriting HTML tags using cfimport
Posted At : April 11, 2007 3:59 PM
| Posted By : Mark Drew
Related Categories:
coldfusion,
tips
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:
<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:
<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)


Thanks for sharing this Mark.
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.
http://mkruger.cfwebtools.com/index.cfm/2006/5/10/...
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. :-)
@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.
Nice tip though!
Cheers
I wasn't trying to say "This is old news".
I was trying to encourage anyone interested in developing this idea further.
Still thinking of more ways to (ab)use this too...