Recursive ColdFusion Custom Tag Example

Posted July 2, 2007 at 8:00 AM

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:

 Launch code in new window » Download code as text file »

  • <!---
  • 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:

 Launch code in new window » Download code as text file »

  • <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:

 Launch code in new window » Download code as text file »

  • <!--- 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).

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

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

History of violence is a hectick movie :D


Jul 3, 2007 at 7:22 AM // reply »
6,371 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 »
73 Comments

Yeah it is a good movie :)


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »