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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »