Determine The Parent ColdFusion Custom Tag Hierarchy Using GetBaseTagList()

Posted April 24, 2007 at 6:39 PM by Ben Nadel

Tags: ColdFusion

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:

  • <cfoutput>
  •  
  • <!---
  • Store the base tag list in content buffer variable.
  • No need for this other than to create nested tags.
  • --->
  • <cfsavecontent variable="strTagList">
  • #GetBaseTagList()#
  • </cfsavecontent>
  •  
  • <!--- Output tag list. --->
  • #strTagList#
  •  
  • </cfoutput>

... 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.

  • <!---
  • Check to see in which mode this tag is currently
  • executing. Most of our actions will only apply to
  • one of the two action modes.
  • --->
  • <cfswitch expression="#THISTAG.ExecutionMode#">
  •  
  • <cfcase value="Start">
  •  
  • <!---
  • Convert the base tag list to an array so that we
  • can more efficiently delete the tags that are NOT
  • custom tags. The GetBaseTagList() function returns
  • tag tag hierachy in an outward order, meaning
  • the current tag is first with the parent tags
  • after that.
  • --->
  • <cfset THISTAG.ParentTags = ListToArray(
  • GetBaseTagList()
  • ) />
  •  
  • <!---
  • Loop over the parent tags and delete all the tags
  • that are not ColdFusion custom tags. Be sure to
  • delete the tags in reverse order so that you do
  • not go out of bounds on the array.
  • --->
  • <cfloop
  • index="intI"
  • from="#ArrayLen( THISTAG.ParentTags )#"
  • to="1"
  • step="-1">
  •  
  • <!---
  • Check to see if this is a custom tag. All
  • ColdFusion custom tags start with "cf_" even
  • is that is not how they are being invoked in
  • the code.
  • --->
  • <cfif (Left( THISTAG.ParentTags[ intI ], 3 ) NEQ "cf_")>
  •  
  • <!--- Delete this standard tag. --->
  • <cfset ArrayDeleteAt(
  • THISTAG.ParentTags,
  • intI
  • ) />
  •  
  • </cfif>
  •  
  • </cfloop>
  •  
  •  
  • <!---
  • At this point, we have cleared out all standard,
  • non custom tags. This very first one in this array
  • will be the current custom tag (remember, our tag
  • list was in an outward direction). We don't need to
  • keep track of this one so delete it.
  • --->
  • <cfset ArrayDeleteAt( THISTAG.ParentTags, 1 ) />
  •  
  • </cfcase>
  •  
  • </cfswitch>

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.




Reader Comments

There are no comments posted for this web log entry.

Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
May 20, 2013 at 10:45 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Ben - I believe you can achieve the same functionality with ColdFusion's built in ArrayToList() function. ArrayToList( users[ "id" ] ); ... read »
May 20, 2013 at 10:21 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Is there any error logging and handling framework in angularjs, if not then in what way I can do this. ... read »
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools