Passing Attribute Values between Custom Tags

Because custom tags are individual templates, variables and other data aren't automatically shared between a custom tag and the calling template. To pass data, you define attributes for the custom tag, just as in standard CFML coding.

To pass data from the calling template to the custom tag, use the ATTRIBUTES scope. Conversely, to pass values back to the calling template, use the CALLER scope. You can also access variables already set on the calling page in the custom tag by simply prefixing the variable with the 'CALLER.' prefix.

Note To create a custom tag:
  1. Create a new application page (the calling page) in Studio.
  2. Modify the file so that it appears as follows:
    <HTML>
    <HEAD>
        <TITLE>Enter Name</TITLE>
    </HEAD>
    
    <BODY>
    <!--- Enter a name, which could also be done in a form --->
    <!--- This example simply uses a cfset --->
    <CFSET NameYouEntered="Smith">
    
    <!--- display the current name --->
    <CFOUTPUT>
    Before you leave this page, you're #NameYouEntered#.<BR>
    </CFOUTPUT>
    
    <!--- go to the custom tag --->
    <CF_GETMD NAME="#NameYouEntered#">
    <!--- come back from custom tag --->
    
    
    <!--- display the results of the custom tag --->
    <CFOUTPUT>
    You are now #DOCTOR#.
    </CFOUTPUT>
    
    </BODY>
    </HTML>
    
  3. Save the page as callingpage.cfm.
  4. Create another new page (the custom tag) in Studio.
  5. Enter the following code:
    <HTML>
    <HEAD>
        <TITLE>GetMD Custom Tag</TITLE>
    </HEAD>
    
    <BODY>
    <!--- get the value of the varible NAME from the calling page --->
    <!--- put the text "Doctor " in front of the name --->
    <!--- create a variable called DOCTOR, make its value "Doctor NAME" -
    -->
    <!--- and make its scope CALLER so that you can pass it back to the 
    calling page --->
    
    <CFPARAM VALUE="Attributes.Name" DEFAULT="Who"
    
    <CFSET CALLER.DOCTOR="Doctor " & "#ATTRIBUTES.NAME#">
    
    </BODY>
    </HTML>
    
  6. Save the page as getmd.cfm.
  7. Open the file callingpage.cfm in your browser.

The calling page uses the getmd custom tag and displays the results.

Code Review
Code Description
<CFSET NameYouEntered="Smith">
In the calling page, create a variable NameYouEntered and assign it the value "Smith."
<CF_GETMD NAME="#NameYouEntered#">
In the calling page, call the getMD custom tag and pass it the NAME parameter whose value is the value of the variable NameYou Entered.
<CFPARAM VALUE="Attributes.Name" 
DEFAULT="Who"
Assign the value "Who" to Name if it has no value.
<CFSET CALLER.DOCTOR="Doctor " & 
"#ATTRIBUTES.NAME#">
See below. (It is helpful to look at this code from right to left.)
#ATTRIBUTES.NAME#
Get the value of the variable NAME from the calling page
<CFSET DOCTOR="Doctor " & 
"#ATTRIBUTES.NAME#">
Create a variable called DOCTOR, make its value "Doctor NAME"
<CFSET CALLER.DOCTOR="Doctor " & 
"#ATTRIBUTES.NAME#">
Make the variable's scope CALLER so that you can pass it back to the calling page

Tip Be careful not to overwrite variables that might already exist on the calling page. You should adopt a naming convention to minimize the chance of overwriting variables. For example, prefix the returned variable with customtagname_, with customtagname being the name of the custom tag.

Note Data pertaining to the HTTP request or to the current application is visible. This includes the variables in FORM, URL, CGI, COOKIES, SERVER, APPLICATION, SESSION, and CLIENT scopes.

The Request scope is a reserved variable/scope that allows you to store data pertaining to the complete hierarchy of custom tags used in a single page request. It is a structure named "request." The variable is available to all templates: base, includes, and custom tags. Collaborating custom tags that are not nested in a single tag can exchange data via the request structure. You should assign a unique name for each variable. You should store data in structures nested inside the request scope.