Custom 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:
Please wish him well.
A custom tag can also have a body and end tag, for example:
<p> Happy Birthday Ellen!</p> <p> May you have many more!</p> </cf_happybirthdayMessge>
Happy Birthday Ellen! May you have many more!
For more information about using end tags, see Handling end tags in Executing custom tags.
You 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.
<head> <title>Date Custom Tag</title> </head> <body> <!--- Call the custom tag defined in date.cfm ---> <cf_date> </body> </html>
- Save the file as callingdate.cfm.
- View callingdate.cfm in your browser. This custom tag returns the current date in the format DD-MMM-YY.
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.
Although tag names in ColdFusion pages are not case sensitive, custom tag filenames must be lowercase on UNIX.
You must store custom tag pages in any one of the following:
- The same directory as the calling page
- The cfusion\CustomTags directory
- A subdirectory of the cfusion\CustomTags directory
- A directory that you specify in the ColdFusion Administrator
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.
You 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:
Required if the name attribute is not used. Same as the template attribute in cfinclude. This attribute:
Required if the template attribute is not used. Use period-separated names to uniquely identify a subdirectory under the CustomTags root directory.Example: <cfmodule name="MyApp.GetUserOptions"> identifies the file GetUserOptions.cfm in the CustomTags\MyApp directory under the ColdFusion root directory.
The custom tag's 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:
For more information on using the cfmessagebox tag, see the CFML Reference.
You 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:
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>
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 in Executing custom 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="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.
The 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.
Before 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.