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 »
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 »
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 »
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
Comments (3) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
ColdFUsion Application.cfc OnRequest() Creates A Component Mixin
CFUNITED Rocks - ColdFusion Rocks - You Guys Rock
History of violence is a hectick movie :D
Posted by Shuns on Jul 2, 2007 at 11:28 PM
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
Posted by Ben Nadel on Jul 3, 2007 at 7:22 AM
Yeah it is a good movie :)
Posted by Shuns on Jul 3, 2007 at 6:28 PM