Recursive ColdFusion Custom Tag Example

Posted July 2, 2007 at 8:00 AM by Ben Nadel

Tags: ColdFusion

I've shown several examples of recursive functions in ColdFusion, but it occurred to me that I have never shown any recursive ColdFusion custom tag examples. ColdFusion custom tags can be invoked from within their own code just the same as a function can. For this example, we will create a ColdFusion custom tag that will output the nodes of an XML document of an undetermined nesting depth. Because the nested child depth is not known, we cannot use a set number of FOR loops to output it. That is where the beauty of recursion comes in; we don't have to know how deep something like an XML document is nested because we will just keep drilling down until we hit a bottom.

To start off, let's build an XML document and pass it into our ColdFusion custom tag for display:

  • <!---
  • Create an XML document that has some information
  • for the very beautiful and highly delicious Maria Bello.
  • By using an XML document, we will be creating an
  • eaily navigatable document for our recursive tag.
  • --->
  • <cfxml variable="xmlMovies">
  •  
  • <movies>
  • <movie>
  • <name>
  • Flicka
  • </name>
  • <releasedate>
  • 2006
  • </releasedate>
  • <imdb>
  • http://imdb.com/title/tt0434215/
  • </imdb>
  • </movie>
  • <movie>
  • <name>
  • Thank You For Smoking
  • </name>
  • <releasedate>
  • 2005
  • </releasedate>
  • <imdb>
  • http://imdb.com/title/tt0427944/
  • </imdb>
  • </movie>
  • <movie>
  • <name>
  • A History Of Violence
  • </name>
  • <releasedate>
  • 2005
  • </releasedate>
  • <imdb>
  • http://imdb.com/title/tt0399146/
  • </imdb>
  • </movie>
  • </movies>
  •  
  • </cfxml>
  •  
  •  
  • <!---
  • Output the XML nodes in our movies document. We are
  • going to pass in the primary XML document and let
  • the tag recursively call itself to display the rest
  • of the nested nodes.
  • --->
  • <cfmodule
  • template="./showxml.cfm"
  • xml="#xmlMovies#"
  • />

Here, we are just creating an XML document of movies staring the crazy insane hot Maria Bello. Then, we hand the XML document object off to the ColdFusion custom tag, showxml.cfm, which outputs the XML document:

  • <movies>
  • <movie>
  • <name>
  • Flicka
  • </name>
  • <releasedate>
  • 2006
  • </releasedate>
  • <imdb>
  • http://imdb.com/title/tt0434215/
  • </imdb>
  • </movie>
  • <movie>
  • <name>
  • Thank You For Smoking
  • </name>
  • <releasedate>
  • 2005
  • </releasedate>
  • <imdb>
  • http://imdb.com/title/tt0427944/
  • </imdb>
  • </movie>
  • <movie>
  • <name>
  • A History Of Violence
  • </name>
  • <releasedate>
  • 2005
  • </releasedate>
  • <imdb>
  • http://imdb.com/title/tt0399146/
  • </imdb>
  • </movie>
  • </movies>

All we had to do was pass off the XML document object to the showxml.cfm tag. The tag, then drills down displaying XML nodes as it finds them:

  • <!--- Kill extra output. --->
  • <cfsilent>
  •  
  • <!---
  • This is the XML object that is going to be
  • displayed. This might be an XML document or
  • it might be an XML node.
  • --->
  • <cfparam
  • name="ATTRIBUTES.Xml"
  • type="xml"
  • />
  •  
  • <!---
  • This is the depth of the XML node. We will
  • need this to indent the XML node on output.
  • --->
  • <cfparam
  • name="ATTRIBUTES.Depth"
  • type="numeric"
  • default="0"
  • />
  •  
  •  
  •  
  • <!---
  • Check to see if the passed in XML object was
  • a document. If it is, then grab the root node
  • reference and store that back into the attribute.
  • --->
  • <cfif IsXmlDoc( ATTRIBUTES.Xml )>
  •  
  • <!--- Get the root node reference. --->
  • <cfset ATTRIBUTES.Xml = ATTRIBUTES.Xml.XmlRoot />
  •  
  • </cfif>
  •  
  •  
  • <!---
  • ASSERT: At this point, no matter what type of
  • XML data was passed into the custom tag, the
  • ATTRIBUTES.Xml variable now points to an XML
  • document node.
  • --->
  •  
  •  
  • <!--- Create a string for the current depth. --->
  • <cfset strDepth = RepeatString(
  • "&nbsp;&nbsp;&nbsp;&nbsp;",
  • ATTRIBUTES.Depth
  • ) />
  •  
  • <!--- Create a string for the next depth. --->
  • <cfset strNextDepth = RepeatString(
  • "&nbsp;&nbsp;&nbsp;&nbsp;",
  • (ATTRIBUTES.Depth + 1)
  • ) />
  •  
  • </cfsilent>
  •  
  • <cfoutput>
  •  
  • <!--- Output the open node tag. --->
  • #strDepth#
  • &lt;#ATTRIBUTES.Xml.XmlName#&gt;<br />
  •  
  • <!---
  • Check to see if this node has child nodes. If it
  • does, we are going to recurse through those. If
  • not, we are just going to display the node text.
  • --->
  • <cfif ArrayLen( ATTRIBUTES.Xml.XmlChildren )>
  •  
  • <!---
  • Loop over the XML children and send each
  • one recursively to this tag.
  • --->
  • <cfloop
  • index="intI"
  • from="1"
  • to="#ArrayLen( ATTRIBUTES.Xml.XmlChildren )#"
  • step="1">
  •  
  • <!---
  • Call the current tag, but this time, pass
  • in the given child of the current XML node.
  • --->
  • <cfmodule
  • template="./showxml.cfm"
  • xml="#ATTRIBUTES.Xml.XmlChildren[ intI ]#"
  • depth="#(ATTRIBUTES.Depth + 1)#"
  • />
  •  
  • </cfloop>
  •  
  • <cfelse>
  •  
  • <!---
  • Output the node text. When outputing this
  • value, use the NEXT node depth as the text
  • will be indented beyond the current tag.
  • --->
  • #strNextDepth#
  • #ATTRIBUTES.Xml.XmlText#<br />
  •  
  • </cfif>
  •  
  • <!--- Output the close node tag. --->
  • #strDepth#
  • &lt;/#ATTRIBUTES.Xml.XmlName#&gt;<br />
  •  
  • </cfoutput>
  •  
  • <!---
  • Exit out to the tag. We only need to fire
  • the open tag. We don't care about any duplicate,
  • close tag functionality.
  • --->
  • <cfexit method="EXITTAG" />

Notice that we only passed in the XML document object to this tag, but the tag itself takes two attributes, XML and Depth. Initially, we don't care about the depth, which will default to zero, but as the custom tag begins to invoke itself recursively, it passes in an incremented depth value so that the output nests, not only syntactically, but also visually.

Once inside the tag, we check to see what type of XML object we have. The first time the tag gets invoked, we are passing in a true XML document object; however, for all subsequent recursive calls, we are passing in an XML node of the original document. In order to make sure that the algorithm can handle the values uniformly, we check to see if we have an XML document and if we do, we replace it with the root XML node of the document. This allows the rest of the code to always assume it has an XML node reference no matter what was passed in.

Then, in the meat of the tag, we check to see if the passed-in XML node has children. If it does, we loop over the children and pass each one recursively back into the current ColdFusion custom tag. Once we hit a node that has no children, we simply output the node value and close out of that instance of the custom tag.

Recursion is a very powerful tool to have and to understand. It really lets you tackle problems that cannot feasibly be done with a set number of loops. But, be cautious because a poorly thought out recursive algorithm can quickly eat up all you RAM bringing your system to a standstill or potentially taking the server down. Sometimes, it is good to build a maximum depth into a recursive algorithm, especially one where not option needs to be explored (such as in artificial intelligence games).


You Might Also Be Interested In:



Reader Comments

Jul 2, 2007 at 11:28 PM // reply »
76 Comments

History of violence is a hectick movie :D


Jul 3, 2007 at 7:22 AM // reply »
11,238 Comments

Yeah, but its a gooood movie. Plus it has a very surprise appearance by William Hurt who is just totally awesome as well.... plus I saw Maria nekid :D


Jul 3, 2007 at 6:28 PM // reply »
76 Comments

Yeah it is a good movie :)


Jun 18, 2010 at 3:27 PM // reply »
3 Comments

Hey Ben-

I'm trying to get this going. I'm in CF6, so that may be my problem, but I'm getting an error: "The required parameter ATTRIBUTES.Xml was not provided."

Any clues?


Jun 20, 2010 at 8:57 PM // reply »
11,238 Comments

@Todd,

That's odd - sounds like you're not passing in the XML attribute to the custom tag.


Jun 21, 2010 at 2:36 PM // reply »
3 Comments

Ahh. Well, I got a little further, but it looks like CF6 won't take the type="xml" portion of the code. It says "Attribute TYPE has an invalid value." This project is on hold until we upgrade.

Thanks for you help anyway, Ben!


Jun 21, 2010 at 3:19 PM // reply »
11,238 Comments

@Todd,

Hmm. You could try changing to type="string" for the CFParam?


Jun 21, 2010 at 4:33 PM // reply »
3 Comments

Genius! WooHoo! Thanks so much!


Jun 21, 2010 at 4:34 PM // reply »
11,238 Comments

@Todd,

Sweeeet :)


Sep 21, 2010 at 6:03 AM // reply »
1 Comments

i appreciate this forum alot.the issue is that there is so much dearth of coldfusion developers out there. is it like coldfusion is not as robust as other languages or whats d issue?


Sep 22, 2010 at 10:12 PM // reply »
11,238 Comments

@Tunji,

The good news is that there are more ColdFusion developers than in the past; the trend is definitely climbing. The language is getting more and more powerful. So, hopefully, the public image will start to improve.


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