The example in this section creates a custom tag that uses an attribute that is passed to it to set the value of a variable called Doctor on the calling page.
<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 #Variables.NameYouEntered#.<br> </cfoutput> <!--- go to the custom tag ---> <cf_getmd Name="#NameYouEntered#"> <!--- Come back from the Custom tag ---> <!--- display the results of the custom tag ---> <cfoutput> You are now #Variables.Doctor#.<br> </cfoutput> </body> </html>
callingpage.cfm
.
<!--- The value of the variable Attributes.Name comes from the calling page. If the calling page does not set it, make it "Who". ---> <cfparam name="Attributes.Name" default="Who"> <!--- Create a variable called Doctor, make its value "Doctor " followed by the value of the variable Attributes.Name. Make its scope Caller so it is passed back to the calling page ---> <cfset Caller.Doctor="Doctor " & Attributes.Name>
getmd.cfm
.
callingpage.cfm
in your browser.
The calling page uses the getmd
custom tag and displays the results.
The following table describes the code and its function:
Code | Description |
---|---|
<cfset NameYouEntered="Smith"> |
In the calling page, create a variable NameYouEntered and assign it the value "Smith." |
<cfoutput> Before you leave this page, you're |
In the calling page, display the value of the NameYouEntered variable before calling the custom tag. |
<cf_getmd Name="#NameYouEntered#"> |
In the calling page, call the getmd custom tag and pass it the |
<cfparam name="Attributes.Name" default="Who"> |
The custom tag page normally gets the Name variable in the Attributes scope from the calling page. Assign it the value "Who" if the calling page did not pass an attribute. |
<cfset Caller.Doctor="Doctor " & Attributes.Name> |
In the custom tag page, create a variable called Doctor in the Caller scope so it will exist in the calling page as a local variable. Set its value to the concatenation of the string "Doctor" and the value of the Attributes.Name variable. |
<cfoutput> You are now #Variables.Doctor#.<br> </cfoutput> |
In the calling page, display the value of the Doctor variable returned by the custom tag page. (This example uses the Variables scope prefix to emphasize the fact that the variable is returned as a local variable.) |