You can use the reserved attribute attributecollection
to pass attributes to custom tags using a structure. The attributecollection
attribute must reference a structure containing the attribute names as the keys and the attribute values as the values. You can freely mix attributecollection
with other attributes when you call a custom tag.
The key-value pairs in the structure specified by the attributecollection
attribute get copied into the custom tag page's Attributes scope. This has the same effect as specifying the attributecollection
entries as individual attributes when you call the custom tag. The custom tag page refers to the attributes passed using attributecollection
the same way as it does other attributes; for example, as Attributes.CustomerName or Attributes.Department_number.
Note: You can use both tag attributes and attributecollections
. If you pass an attribute with the same name using both methods, ColdFusion passes only the tag attribute to the custom tag and ignores the corresponding attribute from the attribute collection.
Custom tag processing reserves the attributecollection
attribute to refer to the structure holding a collection of custom tag attributes. If attributecollection
does not refer to such a collection, ColdFusion generates a template exception.
The following example uses an attributecollection
attribute to pass two of four attributes:
<cfset
zort=StructNew()> <cfset
zort.x = "-X-"> <cfset
zort.y = "-Y-"> <cf_testtwo a="blab" attributecollection=#zort# foo="16">
If testtwo.cfm contains the following code:
---custom tag ---<br> <cfoutput>#attributes.a# #attributes.x# #attributes.y# #attributes.foo#</cfoutput><br> --- end custom tag ---
its output is the following statement:
---custom tag --- blab -X- -Y- 16 --- end custom tag ---
One use for attributecollection
is to pass the entire Attributes scope of one custom tag to another. This often happens when you have one custom tag that calls a second custom tag and you want to pass all attributes from the first tag to the second.
For example, you call a custom tag with the following code:
<cf_first attr1="foo" attr2="bar">
To pass all the attributes of the first custom tag to the second, you include the following statement in first.cfm:
<cf_second attributecollection="#attributes#">
Within the body of second.cfm, you reference the parameters passed to it as follows:
<cfoutput>#attributes.attr1#</cfoutput> <cfoutput>#attributes.attr2#</cfoutput>