Mark Drew (Redux)- cf_etc...

a compendium of railo, cfml, cfeclipse and technology topics

Mark Drew (Redux)- cf_etc...

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

    I'm not sure where I would ever use this. But it's so frickin cool that I want to do this somewhere.

    Thanks for sharing this Mark.
  • 2 Mark Drew // Sep 22, 2008 at 4:13 PM

    If you ever get to a stage where you are parsing any field (such as adding spry to divs for example?) it makes it a lot easier.
  • 3 Phillip Senn // Sep 22, 2008 at 4:13 PM

    I remember toying with this about a year ago.
    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

    The ColdFusio Muse mused about this on May 10, 2006.

    http://mkruger.cfwebtools.com/index.cfm/2006/5/10/adaptive.tags
  • 5 Dan Sorensen // Sep 22, 2008 at 4:13 PM

    Mark: this is brilliant! I've been looking for a way to do this programatically. I modified your custom tag slightly to check if a given link points outside my server and appends an offsite link icon to outgoing links.

    Replace your executionmode IS start with the following:
    &lt;cfset linkStyle = &quot;normalLink&quot;&gt;
       
    &lt;cfif FindNoCase(&quot;http://&quot;, attributes.href) AND NOT FindNoCase(cgi.SERVER_NAME, attributes.href)&gt;
    &lt;cfset linkStyle = &quot;offsiteLink&quot;&gt;
    &lt;/cfif&gt;
    &lt;cfoutput&gt;
    &lt;a href=&quot;#attributes.href#&quot; class=&quot;#linkStyle#&quot;&gt;
    &lt;/cfoutput&gt;
    &lt;/cfif&gt;

    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

    @Phillip: I didnt mean for it to be groundbreaking, it was a solution for a question I was asked today, but then we decided to do something else (there was a different problem) and I realised Tim Buntel showed us this ages ago (when CFMX 6 come out) ... and its strange that I dont see more peope (ab)using this technique!

    @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

    cfimport is very nice, I just hate having to use it in every single template. There should be a way to globally call it.

    Nice tip though!

    Cheers
  • 8 Phillip Senn // Sep 22, 2008 at 4:13 PM

    My comments were to reinforce that if a reader were interested in this topic, there was another blog post for more information.
    I wasn't trying to say &quot;This is old news&quot;.
    I was trying to encourage anyone interested in developing this idea further.
  • 9 Mark Drew // Sep 22, 2008 at 4:13 PM

    Point taken Phillip ;)

    Still thinking of more ways to (ab)use this too...
  • 10 Hive // May 27, 2010 at 9:31 PM

    If you overwrite tags like this, are you having a template for every single HTML-Tag? I tried this in Railo 3.1.2 and after importing a directory with an empty prefix, i get error messages using simple HTML Tags like h1, stating that h1.cfm|cfml wasn't found.
  • 11 pphfnhbha // Jul 25, 2011 at 11:05 AM

    BCVozg <a href="http://jgfjazkafkfc.com/">jgfjazkafkfc</a>;, [url=http://mltaclpmkmqt.com/]mltaclpmkmqt[/url], [link=http://vfltqlfgfcfo.com/]vfltqlfgfcfo[/link], http://jacnmolimkum.com/