User Guide

178 Chapter 10 Reusing Code
Custom tag example
In this example, we create 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.
To create a custom tag:
1 Create a new application page (the calling page) in ColdFusion 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, youre #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>
3 Save the page as callingpage.cfm.
4 Create another new page (the custom tag) in ColdFusion Studio.
5 Enter the following code:
<!--- 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>
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.