Custom Tags


In the previous section, you saw how simple it was to call some of the built-in tags such as the CFOUTPUT and CFSET tags. Because the behaviors of these tags are predefined, adding a built-in CFML tag to your code is as easy as adding a standard HTML tag. One of the greatest features of ColdFusion, however, is its extensibility. This means that you can extend CFML beyond the capabilities of the built-in tags by creating your own custom tags, which work just like standard CFML tags. This means that you can create your own tags that contain functionality that can be reused over and over in any ColdFusion pages.

If you're not up to authoring your own custom tags yet, literally thousands of custom tags are available through the ColdFusion Exchange at www.macromedia.com, many of them freeware or shareware. Before you go and spend a lot of time creating a custom tag, it's usually a good idea to browse the Exchange to see whether someone else has already done the work for you. No need to reinvent the wheel!

Custom Tag Basics

When you create a custom CFML tag, it is stored in its own separate file. Although there are several ways of invoking a custom tag, the easiest is simply to use a tag with the name of the file, with cf_ in front of it. For example, if the custom tag is contained in a file named lastmodified.cfm, you would invoke the custom tag by typing cf_lastmodified.cfm. You can also invoke a custom tag with the cfmodule and cfimport functions. They are often favored for managing a large number of custom tags because they are capable of handling any custom tag name conflicts that might arise.

Note

There are two kinds of custom tags: CFML and CFX. CFX tags are written in C++ or Java. Only CFML tags are covered in this book.


ColdFusion looks for custom tags first in the same directory as the calling page, then in the CFusionMX\CustomTags directory, then in subdirectories of the CFusionMX\CustomTags directory, and finally in directories that you can specify in the ColdFusion MX Administrator.

Creating a Custom Tag

Creating your own custom tag might sound like a daunting task, but keep in mind that custom tags usually consist of code that you might be writing over and over again (hence the need for a custom tag). So if you're going to build the same code block over and over, you might as well create a custom tag instead and save yourself some time.

The first step in creating a custom tag is to determine what it is you want to accomplish. For instance, suppose you wanted every page to display a "last updated on" text block that also included the date when the page was last updated. You could open a text editor and build the following code block:

<CFPARAM name="attributes.lastmodified" default=""> <CFOUTPUT> <I>This page was last updated on #attributes.lastmodified#</I> </CFOUTPUT>


Essentially, this code block takes a date that is passed by the calling page and displays the code block and the date. To invoke the custom tag, you would call it in a CFML page by using code similar to the following:

<HTML> <HEAD> <TITLE>My Page</TITLE> </HEAD> <BODY> Type your content here. <CFLastModified LastModified="June 6, 2005"> </BODY> </HTML>


This page now passes the date on which the page was last modified and it is then displayed, along with the text block, on the page. Although this example may seem a bit remedial, it does demonstrate that each custom tag requires two fundamental things:

  • The defined custom tag code

  • A page that calls that tag and passes any parameters to it.

Truly, the best way to fully understand how custom tags work is to build a few remedial tags yourself, and then spend time on the Macromedia Exchange downloading, reviewing, and looking at the code involved in each of the custom tags on the Exchange.



Special Edition Using Macromedia Studio 8
Special Edition Using Macromedia Studio 8
ISBN: 0789733854
EAN: 2147483647
Year: 2003
Pages: 337

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net