Ok, there are three basic ways to call Custom Tags in ColdFusion.
![]()
<cf_NameOfTag>
This is by far the simplest, but is quite limiting in that it relies on the custom tag file being in the appropriate place in order to be found. Also, when calling tags in this fashion, it becomes impossible to have more than one custom tag with the same name.
<cfmodule>
Up until recently, I used this approach with the
templateattribute. It works perfectly well, but to me, it’s just not aesthetically pleasing looking at several<cfmodule>’s and</cfmodule>’s all over the place. However, this does solve the problem of cf_.
<cfimport> with Xml Namespace reference
- Use the
<cfimport>tag to act as a reference to the directory which contains the custom tag(s) you’ll need on your page (if you need custom tags in multiple directories, simply include a<cfimport>for each directory). For each, assign a unique and meaningful prefix.- To call the custom tag, simply reference the appropriate prefix followed by a “:”, then the name of the tag. Attributes can be passed in however you wish.
- Example:
- Let’s say my page has three custom tags I need, wrapper.cfm which exists in a subdirectory called “customtags/common”, head.cfm and body.cfm in a subdirectory called “customtags/secure”.
<— Use cfimport to get a reference to the directories which contain the custom tags you will call on this page —>
<cfimport prefix=“common” taglib=“customtags/common” />
<cfimport prefix=“secure” taglib=“customtags/secure” /><— Using the prefixes which correspond to the appropriate tag, simply call the tags as follows —>
<common:wrapper>
<secure:head title=”Hello World” />
<secure:body>
Hello World
</secure:body>
<common:wrapper>
The end result, is more readable, XML-esque code. By looking at this, I can easily tell that my wrapper tag, is part of some common package that is probably used by all of my application(s). The head and body tags are part of a package called secure, which possibly means that they are only being used in the secure part of my application. One of the reason ColdFusion is such a wonderful language, is because the code, when written appropriately, is extremely readable. And of all solutions for calling customtags, this is by far the most readable and easiest to maintain.











