Using Advanced Custom Tags ColdFusion Help

When ColdFusion Application Server 4 was released Allaire added new functions and tags to the core language but more importantly they added some interesting new features that directly concern custom tags. These include new ways of calling custom tags, new methods of tag interaction and other features. We will discuss each of these features in detail

Using End Tags

The first major enhancement to custom tags is the end tag. Let’s take a look to get a better idea of what we mean by end tag. Imagine a custom tag that simply inserts a bold tag. Whenever the tag is run, it outputs <B>:
<CF_Bold>Text I want bold

To create proper HTML though you need to send a </B> to end the bold layout. So let’s modify the imaginary tag. Assume that if you send an attribute called Mode and if it is set to End the tag will output a </B> tag. Here is how the template may look

<CF_Bold>Text I want to bold.<CF_Bold MOOE=’End’>

This works all right but is not very pretty. As you know HTML uses end tags to mark the end of a particular markup. From version 4 Coldfusion enables you to do the same thing. Imagine the following in your template

<CF_Bold>Text I want to bold.</CF_Bold>

• ThisTag.ExecutionMode
• ThisTag.H.asEndTag
• ThisTag.GeneratedContent
• “ThisTag.AssocAttribs

Let’s build “areal-life example based on a sifuationmore likely to occur when building Web sites. Most Web sites have a site-wide look and feel to them. Perhaps all content is wrapped in a Verdana font that is sized a bit small. Normally you would build all your “content in the following way:

<FONT FACE-‘Verdana’ SIZE~-l>
The content of our Web site is ~xtremely interestin”g. You will continue to visit the site because every time you visi~. my advertisers spend more money on me.

Even if the content of the site is dynamic, you will probably have something like this in your template:

<FONT FACE-‘Verdana’ SIZE–l>#IntroParagraph#</FONT>

As you can well imagine, a potential problem arises if the art department decides that Verdana is just so early ’99 and the Web site really needs a Y2K look. Let’s then build a ” custom tag that will handle the markup. You can then easily change the look of the site with little to no work on your part every time the art department throws a fit. Listing 31.7 shows the source code of your new custom tag CF_Markup



Using ThisTag GeneratedContent

Another new feature of custom tags starting with ColdFusion 4 is the Thi sTag .GeneratedContent variable, As you might be able to guess, this tag gives you access to the content that has been generated by the tag. This is useful therefore only where Thi sTag. Executi onMode is End. If you think about it no content has been created when the tag starts only when it ends. The same applies to tags without a closing end tag. This handy feature enables you to make changes to whatever content exists between the starting and end tags. You can quickly apply this to Markup. cfm to create a simpler version. Listing 31.9 shows Markup_2. cfm, a new version of the tag showing off the use of Thi sTag. GeneratedContent.

Start’, and second to see if you have forgotten the end tag. If this statement as a whole is False, ColdFusion will skip the contents of the clause. But you cannot use a simple CFELSEtag because you could still be in the •Start’ Executi onModeif you did not forget the end tag. Therefore, you use CFELSEIFand make sure you are at the end of your tag. Once again you can find an example template calling this custom tag on the CD. (The filename is markup_test_3. cfm.)

Posted on November 16, 2015 in Building ColdFusion Custom Tag

Share the Story

Back to Top
Share This