Coldfusion Tutorials



Spry Tutorials

Air Tutorials


Coldfusion Custom Tags

Coldfusion Custom Tags

Coldfusion custom tags are a great way of extending the core set of tags that come with coldfusion and prior to components was the only real way of extending the functionality of coldfusion. I still think it's a great way to reuse code and reduce the amount of code you write for a given site.

I typically use custom tags for repetitive bits of code that, while not necessarily difficult may be considered mundane tasks that take time away from more important tasks. A coldfusion custom tag is simply a .cfm page that encapsulates a bit of code you want to use in a page. You simply create your page and then either save it in the directory you are going to use it or in the Custom Tags directory on the coldfusion server (you can also call it using the <cfmodule> tag explained later).

To call your tag you simple do the following <cf_myCustomTag> and whatever code you have inside your myCustomTag.cfm page will be executed on the calling page.

I should also note that a custom tag can have an end tag as well, this allows you the possibility perform an action on whatever is between the start and end tag, called the body. For example:

<cf_titleCase>change the text to title text</cftitleCase>

would result in the following:

Change The Text To Title Text 

The Code for titlecase.cfm:

Dissecting the Code:

  1. Lines 2-6: The first thing we do is make sure that there is both an open and close tag. By setting the thisTag.generatedContent="" we won't output the enclosed code as well as our warning text.
  2. Line 8: Here we make sure that we only execute this code when we come across the end tag, otherwise the code would execute for the open tag as well.
  3. Line 11: We loop through the text that is between the open and close tag by treating the string as a list delimited by spaces this way we get one word to work on each time through the loop.
  4. Line 12: Is were all the work is done. On this line we the ucase function uppercase the first character of each word by using the mid function to extract the first letter of the word, we then append the rest of the word by using another mid function.
  5. Line 13: We rebuild the original string with our new words and append a space back on to the end of each word.
  6. Line 16: After the end of the loop we will have one extra space at the end of the string so lets remove it.
  7. Line 18: The new string is outputted to the screen.

Explanation of thisTag Scope
There are four attributes that are available to you when you build a custom tag:

ExecutionMode: Contains the execution mode of the custom tag. Valid values are "start", "end", and "inactive".
HasEndTag: Distinguishes between custom tags that are called with and without end tags. Used for code validation. If the user specifies an end tag, HasEndTag is set to True; otherwise, it is set to False.
GeneratedContent: The content that has been generated by the tag. This includes anything in the body of the tag, including the results of any active content, such as ColdFusion variables and functions. You can process this content as a variable.
AssocAttribs: Contains the attributes of all nested tags if you use cfassociate to make them available to the parent tags. This is generally used for more advanced custom tags