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>
<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>
        <cfset currentDepth = currentDepth - 1>
    <cfoutput><li><a href="#relativeDir#/#name#">#normalizedName#</a></li></cfoutput>



  1. Can you please explain how to define the site root in an application variable?  I've searched all over for this and this is the main site that is returned in Google searches.

    1. I’m not sure this answers your question, but within your Application.cfc onApplicationStart method, you can place a variable as such:

      application.strConfig = StructNew();
      application.strConfig.siteRoot = '/var/www/html/mysite_com/www';

      This variable will then be available to all pages with each request, and have a lifetime equal to that of your Applications timeout value, after which, it will reinitialize. This is a good practice for constants.

Leave a Reply

Your email address will not be published. Required fields are marked *