Up until now, I have worked with ColdFusion custom tags that have a known parent; my cell has always been a child of the row, my row has always been a child of the table. But what happens if you want to build a slightly more dynamic tag that doesn't necessarily know what it's parent tag hierarchy is? Determining this hierarchy has been done loads before (which I am sure most of you know), but I have never done it, so I thought I would experiment.
You can use the GetBaseTagList() to get a comma delimited list of the current ColdFusion tag's parent tags. This list starts with the current tag (so it's a bit more than just a parent tag list, unless you consider the fact that all tags are parent to the actual method call). This function can be used outside of ColdFusion custom tags; it can be used anywhere in ColdFusion code. Running this:
Launch code in new window » Download code as text file »
... gives us:
CFSAVECONTENT,CFOUTPUT
Notice that the GetBaseTagList() starts with CFSaveContent, the current tag, and then includes the parent tags (of which there is only one, CFOutput).
Of course, doing this outside of ColdFusion custom tags is not all that exciting. What this does show us though, is that GetBaseTagList() returns not just custom tags but all parent ColdFusion tags. Because of this, we have to be careful of its use in custom tags, especially when we only want a list of parent custom tags, not parent ColdFusion tags.
Here is a generic ColdFusion custom tag that will build an array of parent ColdFusion custom tags. Since GetBaseTagList() returns a bottom-up list of tags, the array that we generate below starts with the closest parent and then travels up to the originating parent tag.
Launch code in new window » Download code as text file »
Notice that as a final step we are deleting the top item from the parent tag array. Again, this is because the GetBaseTagList() includes the current tag as well as all parent tags. This is a lot of work for a relatively simple idea. ColdFusion is very awesome and has been thought out very well; this makes me think that gathering parent tag data is not something that is advisable. Otherwise, I assume they would have built in something that accomplished this a bit more readily.
Download Code Snippet ZIP File
Comments (0) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Creating One-Way Full Data Communication For ColdFusion Custom Tags With CFAssociate
Lenny And Bo, ColdFusion Programmers (Vol. 9)
There are no comments posted for this web log entry.