•
Use the
cfparam
defaults for optional attributes that are passed from a calling page. For example:
<!--- 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">
•
Use the
cfparam
test for required attributes that must be passed from a calling page; for example, the following
code issues an abort if the user does not specify the Name attribute to the custom tag:
<cfif not IsDefined("Attributes.Name")>
<cfabort showError="The Name attribute is required.">
</cfif>
Custom tag example with attributes
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.
To create a custom tag:
Create a new ColdFusion page (the calling page) with the following content:
1
<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>
Save the page as
2
Create another new page (the custom tag) with the following content:
3
<!--- 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">
tag with a
attribute at the top of a custom tag to test for and assign
default
tag or a
tag with an
cfif
.
callingpage.cfm
"
. --->
function at the top of a custom tag to
IsDefined
"
. --->
Passing data to custom tags
179
Need help?
Do you have a question about the COLDFUSION MX 61-DEVELOPING COLDFUSION MX and is the answer not in the manual?
Questions and answers