Storing Child ColdFusion Custom Tag Data Within A Parent Using GetBaseTagData()

Posted April 19, 2007 at 7:19 PM by Ben Nadel

Tags: ColdFusion

If you have started working with ColdFusion custom tags, you probably have used the CFAssociate tag. This tag stores the current custom tag's ATTRIBUTES scope in an array of the associated tag. But what happens if you want to store more than just the ATTRIBUTES scope? What happens if you want to store a pointer to each nested tag within the parent tag?

This type of full communication can be done through the GetBaseTagData() method. GetBaseTagData() takes a base tag name (and optional instance number) and returns a pointer to the base tag's VARIABLES scope. This VARIABLES scope, in turn, gives you access to the THISTAG, ATTRIBUTES, and CALLER scopes of the base tag. Once we have this, we can easily store things in the parent tag (base tag) from within the child tag (nested tag).

As a simple experiment, I have parent tag that has one level of child tags. Each child tag stores itself within the parent. Then, once the parent tag is done executing, it just outputs the number of child tags that it has encountered. Here is the demo:

  • <!--- Impor the tag libraries. --->
  • <cfimport taglib="./" prefix="tag" />
  •  
  • <!--- Define the parent tags with 5 children. --->
  • <tag:parent>
  •  
  • <tag:child />
  • <tag:child />
  • <tag:child />
  • <tag:child />
  • <tag:child />
  •  
  • </tag:parent>

Not much going on there. Now, let's take a look at the Parent tag (parent.cfm):

  • <!---
  • Check to see which execution mode the tag is running in.
  • We won't have access to the child tag data until we are
  • in the End tag mode.
  • --->
  • <cfswitch expression="#THISTAG.ExecutionMode#">
  •  
  • <cfcase value="Start">
  •  
  • <!---
  • Define the array or children. This array will be
  • populated by the child custom tags.
  • --->
  • <cfset THISTAG.Children = ArrayNew( 1 ) />
  •  
  • </cfcase>
  •  
  •  
  • <cfcase value="End">
  •  
  • <!---
  • At this point, all of our nested Child custom tags
  • will have executed. Output the number of children
  • that we have nested. Since each child tag appends
  • itself to the Children array above, this should
  • be an accurate number.
  • --->
  • <cfoutput>
  • I Found #ArrayLen( THISTAG.Children )# Children.
  • </cfoutput>
  •  
  • </cfcase>
  •  
  • </cfswitch>

Again, not much going on here. In the Start mode of the parent tag, we are creating an array to hold pointers to all of the child custom tags. Then, in the End mode of the parent tag, all we have to do is echo out the length of that children array.

Running the above, we get the following output:

I Found 5 Children.

It works nicely. Now, let's take a look at the Child tag to see how each child is subsequently updating the parent's tag data:

  • <!---
  • Check to see which execution mode the tag is
  • running in.
  • --->
  • <cfswitch expression="#THISTAG.ExecutionMode#">
  •  
  • <cfcase value="Start">
  •  
  • <!---
  • Get the parent tag, which is our base tag. Remember
  • that even though our file name is "parent.cfm",
  • since it is being used as a custom tag is uses the
  • "cf_" notation and not file extension.
  • --->
  • <cfset THISTAG.Parent = GetBaseTagData( "cf_parent" ) />
  •  
  •  
  • <!---
  • Add child to parent's child array. By adding the
  • VARIABLES scope, we will be giving the parent tag
  • access to every aspect of this tag including the
  • ATTRIBUTES, CALLER, and THISTAG scope.
  • --->
  • <cfset ArrayAppend(
  • THISTAG.Parent.THISTAG.Children,
  • VARIABLES
  • ) />
  •  
  • </cfcase>
  •  
  • </cfswitch>

Here, the child is getting a pointer to the base tag's VARIABLES scope using the GetBaseTagData() method. Remember that since the VARIABLES scope is a ColdFusion struct it is passed by reference, not by value. That means that we are now sharing the VARIABLES struct with the base tag - we do not have a copy of it. Once we get this pointer, we just append the current child tag's VARIABLES scope to the Children array that we defined in the Start mode of the parent tag.

Using the GetBaseTagData() we can really get a good bidirectional chain of communication. With the pointer we get from the GetBaseTagData() call, we can communicate with the base tag. Then, with the VARIABLES pointer we are storing back into the parent tag's memory space, the parent tag can easily communicate with the child tags.

One word of caution here: This storage creates circular object references; the parent tag now has pointer to the child tags, each of which have a pointer to the parent, which has pointers to the child tags, each of which have a pointer to the parent... etc. etc. etc. What this means is that if you try to CFDump out ANY of this stuff, you will kill your stack and possibly bring down your machine :) Ok, maybe that's a bit dramatic, but please, if you do use CFDump with this kind of experiment, be sure to use the TOP attribute - this will limit the damage that can be caused by the infinite level of dumping.




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 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 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... 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 »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools