When you are using nested ColdFusion custom tags, you can use the CFAssociate tag to associate the child tags with a parent tag. By default, this tag stores the ATTRIBUTES scope values in the parent tag in an array named AssocAttribs. You can, however, explicitly name the target array using the datacollection attribute.
Now, the ATTRIBUTES structure is meant to hold the attributes that are passed via tag attributes when the tag is invoked. But, ATTRIBUTES, like almost every other structure in ColdFusion, can be modified in any way that the programmer desires. This got me thinking - what if you store a pointer to the current tag in the ATTRIBUTES scope? What does the parent tag see?
To test this out, I have a few nested tags:
Launch code in new window » Download code as text file »
As you can see, there are several Girl.cfm custom tags nested within the Girls.cfm ColdFusion tag. Let's take a look at the Girl.cfm ColdFusion tag to see how it is being associated with the parent tag:
Launch code in new window » Download code as text file »
Using the CFAssociate ColdFusion tag, Girl.cfm is associating its attributes with the Girls.cfm tag. We are explicitly telling the parent tag (Girls.cfm) to store these associated attributes in a data array names "Girls." But, then, we do something a little sneaky - we store a VARIABLES pointer right into the ATTRIBUTES scope with the key, TagContext.
Let's see what this does for the parent tag (Girls.cfm):
Launch code in new window » Download code as text file »
Running the above ColdFusion custom tags, we get the following CFDump output:
| | | | ||
| | ![]() | | ||
| | | |
When CFDumping out the Girls associate attributes array, each tag as the attribute TagContext which is a pointer to the nested child tag itself. Now, remember, since ColdFusion passes structures by reference, and we passed in the VARIABLES struct (scope), this tag that is showing up in the CFDump provides a direct chain of communication with the nested tag, not a copy of it. The parent tag, at this point, could reach in using the TagContext pointer and manipulate or read the child tag's data, if it so desired.
If you only need the parent tag to be able to reach into the child tag (and not the other way around), this feels like a very elegant solution. It took two tags and a slight misuse of the ATTRIBUTES scope. This seems much better than trying to get the base tag data and storing a reference into that. This just seems somehow more organized.
Just a word of caution: since you are storing a tag pointer into its own ATTRIBUTES scope, this creates circular structure references. If you try to CFDump this out without using the Top attribute of the CFDump tag, you will cause an infinitely looping output (which may crash your machine).
Download Code Snippet ZIP File
Comments (2) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Determine How Long Your ColdFusion Page Has Been Running Using GetPageContext()
Determine The Parent ColdFusion Custom Tag Hierarchy Using GetBaseTagList()
Ben,
If you really want to see some excellent examples of nesting custom tags and what not, download the cfobjects framework:
http://cfobjects.sourceforge.net/
This framework was around long, long ago before CFCs. It was a way to use OO programming in ColdFusion before it had it natively. The way it worked was by using custom tags as classes and methods. Check it out.
Posted by tony petruzzi on Apr 25, 2007 at 11:14 AM
@Tony,
That some pretty complex stuff. I just downloaded the files and have been poking around and reading through the getting started. Some very interesting techniques. Thanks, this will definitely get me thinking :)
Posted by Ben Nadel on Apr 26, 2007 at 7:25 AM