Custom Tag Attributes


A custom tag is defined in a single ColdFusion page. You cannot access a variable created in the calling page directly from the custom page, and vice versa. Custom tag attributes allow you to share information between the calling page and the custom tag page.

Passing Attribute Values to Custom Tags

You can pass data from a calling page to a custom tag page by specifying attributes in the form of name/value pairs in the custom tag. For example, you can specify an attribute in the custom tag <cf_product> by using the following code:

 <cf_product productname="Product 1"> 

In <cf_product>, the attribute productname is created with the value Product 1 and is passed to the custom tag from the calling page. You can access productname in the custom tag page by using the prefix Attributes before the name of the attribute. The prefix Attributes specifies that the attribute passed to the custom tag page from the calling page has Attributes scope.

Place the following code in the custom tag page to retrieve the value of productname passed to it:

 <CFSET myproductname='#Attributes.productname#'> 

The following code creates a ColdFusion page that calls a custom tag and passes attribute name/value pairs to the custom tag page:

 <HTML> <HEAD> <Title> Passing Attributes to a Custom Tag Page</Title> </HEAD> <BODY>     Following is an example of passing attribute values to a     custom tag page<br>     <cf_showprofile name="Robert Lundgren"         age="32"location="Tennessee"> </BODY> </HTML> 

The custom tag page uses the following code to retrieve the values of the attributes passed from the calling page:

 <cfset candidatename="#Attributes.name#"> <cfset candidateage="#Attributes.age#"> <cfset candidatelocation="#Attributes.location#"> <CFOUTPUT>     The name of the candidate is #candidatename#<br>     The age of the candidate is #candidateage#<br>     The candidate's location is #candidatelocation#<br> </CFOUTPUT> 

You can also use the <cfmodule> tag to call a custom tag page and pass attributes to the custom tag page:

 <HTML> <HEAD> <Title> Passing Attributes to a Custom Tag Page</Title> </HEAD> <BODY> Following is an example of passing attribute values to a custom tag page using the cfmodule tag<br> <cfmodule template="/showprofile.cfm" name="Robert Lundgren" age="32"location="Tennessee"> </BODY> </HTML> 

Note

Attribute names aren't case-sensitive.

Passing Values to a Calling Page from a Custom Tag

The custom tag page can also access the variables defined in the calling page by using the Caller prefix before the name of this variable.

The variables created in a custom tag page are deleted or purged after the custom tag page has been processed. The Caller prefix helps you access the variable defined in the calling page and pass information from the custom tag page back to the calling page.

Note

It's not possible to access the variables in a custom tag outside the scope of the custom tag page.

The following code creates a ColdFusion page that calls a custom tag page changename.cfm and passes attribute name/value pairs to it:

 <HTML> <HEAD> <Title> Passing Attributes to a Custom Tag Page</Title> </HEAD> <BODY>     <cfset newname="Richard">     <cfset newlocation="Wisconsin">     <p>     <font color=red>The value of the variables before the       execution of the custom tag page is:</font><br>     <cfoutput>       The newname variable contains the following value:         #newname#<br>       The newlocation variable contains the following value:         #newlocation#     </cfoutput>     </p>     <p>       <!-- Following is an example of passing attribute values           to a custom tag page -->       <cf_changename newname="Robert Lundgren"           newlocation="Tennessee">       <font color=red>The value of the variables after the         execution of the custom tag page is:</font><br>       <cfoutput>         The newname variable contains the following value:             #newname#<br>         The newlocation variable contains the following value:             #newlocation#       </cfoutput>     </p> </BODY> </HTML> 

The custom tag page changename.cfm changes the values of the variables in the calling page. It can be created using this code:

 <cfset customname="#Attributes.newname#"> <cfset customlocation="#Attributes.newlocation#"> <!-- change the value of the variables in the calling page--> <cfset Caller.newname= customname> <cfset Caller.newlocation= customlocation> 




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