Custom Tag Execution


When a custom page is executed, all the data related to the custom tag is stored in the thisTag structure. You can access thisTag from within a custom tag to control its processing. thisTag contains variables or properties that determine the execution mode and the structure of the custom tag. Table A.1 lists the important variables in thisTag:

Table A.1: Variables In the thisTag Structure

Variable

Used for

ExecutionMode

Determines the execution mode of the custom tag. This variable can contain one of these values: start, end, and inactive.

HasEndTag

Determines whether the custom tag declaration in the calling page has an end tag or not. If the custom tag has an end tag, this variable returns a TRUE value. Otherwise, it returns a FALSE value.

GeneratedContent

Accesses the body text of the custom tag. This includes text, values from ColdFusion variables, and any result of processing in the custom tag.

Handling End Tags

When you define an end tag in a custom tag, the custom tag page is called twice, first for the start tag and the second time for the end tag. The ExecutionMode variable determines whether the call has been made for the start tag or the end tag. You can also use HasEndTag of thisTag of a custom tag to determine whether an end tag has been specified for the custom tag.

The following code creates a ColdFusion page that references a custom tag containing an end tag:

 <HTML> <HEAD> <Title> A custom tag containing an end tag</Title> </HEAD> <BODY>     <cf_customtag1 name="Terry">       <!-- This custom tag contains an end tag -->     </cf_customtag1> </BODY> </HTML> 

You can use the following code to verify whether the custom tag declared in the calling page contains an end tag:

 <font color=red> <cfset name="Attributes.name"> <cfif thisTag.ExecutionMode is 'start'>     <cfif thisTag.HasEndTag is 'True'>       The custom tag has an end tag<br>     <cfelse>       The custom tag does not an end tag<br>     </cfif> </cfif> </font> 

You can use thisTag.HasEndTag to validate whether a custom tag has an end tag. For example, this code validates a custom tag call and generates an error if the custom tag does not contain an end tag:

 <cfif thisTag.HasEndTag is 'False'>     <cfabort showError="The end tag is missing"> </cfif> 

Handling Body Text in a Custom Tag

When you call a custom tag with an end tag, the text between its start and end tags is known as the body text. For example, the following custom tag call includes body text between the start and end tags:

 <cf_mytag myname="Richard Duchovny">     I am working with XYZ company as a Developer.     My area of specialization is Microsoft Technologies </cf_mytag> 

You can use thisTag.GeneratedContent to access the body text within a custom tag call from within the custom tag page. This variable contains all the body text contained within a custom tag.

Note

Since the thisTag.GeneratedContent variable remains empty during the processing of the start tag, you cannot use it to access the body text passed to a custom tag page.

For example, the following code creates a ColdFusion page that calls the custom tag page mybody.cfm and passes text to the body of the custom tag:

 <HTML> <HEAD> <TITLE>A custom tag containing body text</TITLE> </HEAD> <BODY>     <cf_mybody>       This is a sample body text within a custom tag     </cf_mybody> </BODY> </HTML> 

You can use this code to create a custom tag page that retrieves the text passed to it using thisTag.GeneratedContent:

 <CFOUTPUT> <CFIF thisTag.ExecutionMode is 'end'>     <!-- Display the body text contained in a custom tag -->     #thisTag.GeneratedContent# </CFIF> </CFOUTPUT> 

You can also use thisTag.GeneratedContent to modify the body text during the processing of the custom tag as follows:

 <CFIF thisTag.ExecutionMode is 'end'>     <!-- Modify the body text contained in a custom tag -->     <cfset thisTag.GeneratedContent="This is the modified content"> </CFIF> 




Macromedia ColdFusion MX. Professional Projects
ColdFusion MX Professional Projects
ISBN: 1592000126
EAN: 2147483647
Year: 2002
Pages: 200

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net