SiteKickr Web Development

ColdFusion dynamic sitemap

I searched the web this morning in hopes that someone has already written a recursive sitemap generator in ColdFusion. It may be out there, but I gave up after browsing page 10 of the Google search for "ColdFusion sitemap". The results were not entirely unexpected – they were all examples of creating a dynamic Google sitemap, which requires no presentation.

In the example below, I use CFDirectory recursively to identify all files within a site, then display them with a nested list.

<!--- the code below assumes that you have defined your site root in an application variable (a smart thing to do for almost all websites) --->
<cfdirectory directory="#application.strConfig.webroot#" action="list" filter="*.cfm" recurse="true" sort="directory ASC, name ASC" name="files">
<cfset currentDepth = 0>
<ul>
<cfloop query="files" endrow="25">
    <cfset relativeDir = Replace(directory, application.strConfig.webroot, "")>
    <!--- "normalize" the filename, in this case, replace all hypens and underscores with spaces,
             then remove the extension --->
    <cfset normalizedName = ListFirst(Replace(Replace(name, "-", " ", "ALL"), "_", " ", "ALL"), ".")>
    <cfif ListLen(relativeDir, "/") gt currentDepth>
        <cfoutput><li>#ListLast(relativeDir, "/")#</cfoutput>
        <cfoutput><ul><li><a href="#relativeDir#/#name#">#normalizedName#</a></li></cfoutput>
        <cfset currentDepth = currentDepth + 1>
    <cfelseif ListLen(relativeDir, "/") lt currentDepth>
        <cfloop from="#ListLen(relativeDir, "/")#" to="#currentDepth#" index="i"></ul></li></cfloop>
        <cfoutput><li>#ListLast(relativeDir, "/")#</cfoutput>
        <ul>
        <cfset currentDepth = currentDepth - 1>
    </cfif>
    <cfoutput><li><a href="#relativeDir#/#name#">#normalizedName#</a></li></cfoutput>
</cfloop>
</ul>