ColdFusion 9.0 Resources |
Creating custom tagsCustom tags let you extend CFML by adding your own tags to the ones supplied with ColdFusion. After you define a custom tag, you can use it on a ColdFusion page just as you would any of the standard CFML tags, such as cfquery and cfoutput. You use custom tags to encapsulate your application logic so that it can be referenced from any ColdFusion page. Custom tags allow for rapid application development and code reuse while offering off-the-shelf solutions for many programming chores. For example, you can create a custom tag, named cf_happybirthday, to generate a birthday message. You could then use that tag in a ColdFusion page, as follows: <cf_happybirthday name="Ted Cantor" birthDate="December 5, 1987"> When ColdFusion processes the page containing this tag, it could output the message: December 5, 1987 is Ted Cantor's Birthday. Please wish him well. A custom tag can also have a body and end tag, for example: <cf_happybirthdayMessge name="Ellen Smith" birthDate="June 8, 1993"> <p> Happy Birthday Ellen!</p> <p> May you have many more!</p> </cf_happybirthdayMessge> This tag could output the message: June 8, 1993 is Ellen Smith's Birthday. Happy Birthday Ellen! May you have many more! For more information about using end tags, see Handling end tags. Creating and calling custom tagsYou implement a custom tag with a single ColdFusion page. You then call the custom tag from a ColdFusion page by inserting the prefix cf_ before the page’s filename. The page that references the custom tag is referred to as the calling page.
As you can see from this example, creating a custom tag in CFML is no different from writing any ColdFusion page. You can use all CFML constructs, as well as HTML. You are free to use any naming convention that fits your development practice. Unique descriptive names make it easy for you and others to find the right tag. Note: Although
tag names in ColdFusion pages are not case sensitive, custom tag filenames
must be lowercase on UNIX.
Storing custom tag pagesYou must store custom tag pages in any one of the following:
To share a custom tag among applications in multiple directories, place it in the cfusion\CustomTags directory. You can create subdirectories to organize custom tags. ColdFusion searches recursively for the Custom Tags directory, stepping down through any existing subdirectories until the custom tag is found. You can have a situation where you have multiple custom tags with the same name. To guarantee which tag ColdFusion calls, copy it to the same directory as the calling page. Or, use the cfmodule tag with the template attribute to specify the absolute path to the custom tag. For more information on cfmodule, see the next section. Calling custom tags with the cfmodule tagYou can also use the cfmessagebox tag to call custom tags if you want to specify the location of the custom tag page. The cfmodule tag is useful if you are concerned about possible name conflicts when using a custom tag, or if the application must use a variable to dynamically call a custom tag at runtime. Use either a template or name attribute in the tag, but you cannot use both. The following table describes the basic cfmodule attributes:
For example, the following code specifies to execute the custom tag defined by the mytag.cfm page in the parent directory of the calling page: <cfmodule template="../mytag.cfm"> For more information on using the cfmessagebox tag, see the CFML Reference. Calling custom tags with the cfimport tagYou can use the cfimport tag to import custom tags from a directory as a tag library. The following example imports the tags from the directory myCustomTags: <cfimport prefix="mytags" taglib="myCustomTags"> Once imported, you call the custom tags using the prefix that you set when importing, as the following example shows: <mytags:customTagName> where customTagName corresponds to a ColdFusion application page named customTagName.cfm. If the tag takes attributes, you include them in the call: <mytags:custom_tag_name attribute1=val_1 attribute2=val_2> You can also include end tags when calling your custom tags, as the following example shows: <mytags:custom_tag_name attribute1=val_1 attribute2=val_2> ... </mytags:custom_tag_name> ColdFusion calls the custom tag page twice for a tag that includes an end tag: once for the start tag and once for the end tag. For more information on how ColdFusion handles end tags, and how to write your custom tags to handle them, see Handling end tags. One of the advantages to using the cfimport tag is that you can define a directory structure for your custom tags to organize them by category. For example, you can place all security tags in one directory, and all interface tags in another. You then import the tags from each directory and give them a different prefix: <cfimport prefix="security" taglib="securityTags"> <cfimport prefix="ui" taglib="uiTags"> ... <security:validateUser name="Bob"> ... <ui:greeting name="Bob"> ... Reading your code becomes easier because you can identify the location of your custom tags from the prefix. Securing custom tagsThe ColdFusion security framework enables you to selectively restrict access to individual tag files and tag directories. This feature can be an important safeguard in team development. For details, see Configuring and Administering ColdFusion. Accessing existing custom tagsBefore creating a custom tag in CFML, review the free and commercial custom tags available on the Adobe developer’s exchange (www.adobe.com/go/learn_cfu_cfdevcenter_en).You might find a tag that does what you want. Tags are grouped in several broad categories and are downloadable as freeware, shareware, or commercial software. You can view each tag’s syntax and usage information. The gallery contains a wealth of background information on custom tags and an online discussion forum for tag topics. Tag names with the cf_ preface are CFML custom tags; those tags with the cfx_ preface are ColdFusion extensions written in Java or C++. For more information about the CFX tags, see Building Custom CFXAPI Tags. If you do not find a tag that meets your specific needs, you can create your own custom tags in CFML. |