User Guide
182 Chapter 10 Reusing Code
The cfassociate tag has the following format:
<cfassociate baseTag="tagName" dataCollection="collectionName"
The baseTag attribute is the name of the base tag that gets access to this tag’s
attributes. The
dataCollection attribute is the name of the structure in which the
base tag stores the sub-tag data. Its default value is AssocAttribs. You only need to
specify a
dataCollection attribute if the base tag can have more than one type of
sub tag. It is convenient for keeping separate collections of attributes, one per tag
type.
When
cfassociate is encountered in a sub tag, the sub tag’s attributes are
automatically saved in the base tag. The attributes are in a structure appended to the
end of an array whose name is 'thisTag.collectionName'.
The
cfassociate tag performs the following operations:
<!--- Get base tag instance data --->
<cfset data = getBaseTagData(baseTag).thisTag>
<!--- Create a string with the attribute collection name --->
<cfset collection_Name = "data.#dataCollection#">
<!--- Create the attribute collection, if necessary --->
<cfif not isDefined(collectionName)>
<cfset #collection_Name# = arrayNew(1)>
</cfif>
<!--- Append the current attributes to the array --->
<cfset temp=arrayAppend(evaluate(collectionName), attributes)>
The CFML code accessing sub-tag attributes in the base tag could look like the
following:
<!--- Protect against no sub-tags --->
<cfparam Name=’thisTag.assocAttribs’ default=#arrayNew(1)#>
<!--- Loop over the attribute sets of all sub tags --->
<cfloop index=i from=1
to=#arrayLen(thisTag.assocAttribs)#>
<!--- Get the attributes structure --->
<cfset subAttribs = thisTag.assocAttribs[i]>
<!--- Perform other operations --->
</CFLOOP>
Ancestor data access
The ancestor’s data is represented by a structure object that contains all the
ancestor’s data.
The following functions provide access to ancestral data:
•
GetBaseTagList() Returns a comma-delimited list of uppercase ancestor tag
names. An empty string is returned if this is a top-level tag. The first element of a
non-empty list is the parent tag.
• GetBaseTagData(TagName, InstanceNumber=1) Returns an object that contains
all the variables (not just the local variables) of the nth ancestor with a given