The following sections provide information on executing custom tags.
When a custom tag page executes, ColdFusion keeps data related to the tag instance. The thisTag
built in structured variable preserves this data with a unique identifier. The behavior is similar to the File
tag-specific variable (sometimes called the File scope).
The following variables are generated by the thisTag
structure:
ColdFusion invokes a custom tag page in either of two modes:
If an end tag is not explicitly provided and shorthand empty element syntax (<TagName .../>) is not used, the custom tag page gets invoked only once, in start tag mode. If a tag must have an end tag provided, use thisTag.HasEndTag
during start tag execution to validate this.
The same CFML page is executed for both the start and end tag of a custom tag.
A variable with the reserved name thisTag.ExecutionMode
will specify the mode of invocation of a custom tag page. The variable has one of the following values:
When the body of the custom tag (not the custom tag page or template) executes, the value of the ExecutionMode variable is inactive.
A custom tag page that performs processing in both modes can have the following format:
<cfif thisTag.ExecutionMode is 'start'> <!--- Start tag processing ---> <cfelse> <!--- End tag processing ---> </CFIF>
<cfswitch expression=#thisTag.ExecutionMode#> <cfcase value= 'start'> <!--- Start tag processing ---> </cfcase> <cfcase value='end'> <!--- End tag processing ---> </cfcase> </cfswitch>
The cfexit
tag terminates execution of a custom tag. The cfexit
tag's method
attribute specifies where execution continues. cfexit
can specify that processing continues from the first child of the tag or continues immediately after the end tag marker.
You can also use the method
attribute to specify that the tag body executes again. This enables custom tags to act as high-level iterators, emulating cfloop
behavior.
The following table summarizes cfexit
behavior:
Custom tags can access and modify the generated content of any of its instances using the thisTag.GeneratedContent
variable. In this context, the term generated content means the results of processing the body of a given tag. This includes all text and HTML code in the body, the results of evaluating ColdFusion variables, expressions, and functions, and the results generated by descendant tags. Any changes to the value of this variable results in changes to the generated content.
thisTag.GeneratedContent
is always empty during the processing of a start tag. Any output generated during start tag processing is not considered part of the tag's generated content.
As an example, consider a tag that comments out the HTML generated by its descendants. Its implementation could look something like this:
<cfif thisTag.ExecutionMode is 'end'> <cfset thisTag.GeneratedContent = '<!--#thisTag.GeneratedContent#-->'> </cfif>