Not yet rated

Problem

Sometimes you may find the need to output a trail of breadcrumbs which simply reflect the directory structure of where you are in the site, including the current template (.cfm file).

Solution

By looping over the cgi.script_name variable, we can cut up the various paths into individual entries which we can then output.

Detailed explanation

<cfscript>
  //Start a new query which we can fill
   qBreadcrumbs=QueryNew("url,title", "varchar,varchar");
   //The starting link
   link="/";
</cfscript>

<cfloop list="#cgi.SCRIPT_NAME#" delimiters="/" index="i">
    <cfscript>
       // Add a row to our query
       QueryAddRow(qBreadcrumbs, 1);
       // Set the row with URL
       QuerySetCell(qBreadcrumbs, "url", link & i);
       // Set the row with the title: If '.cfm' exists, remove it, and replace underscores with spaces
       // You could also use the TitleCase() function (see http://lab.artlung.com/coldfusion-titlecase/ ) to auto capitalise
       QuerySetCell(qBreadcrumbs, "title", "#replace(replace(i, '_', ' ', 'all'), '.cfm', '', 'all')#");
       //Update the link location for the next loop
       link=link & i & '/';
    </cfscript>
</cfloop>

<cfoutput>
<div id="breadcrumbs"><cfif qBreadcrumbs.recordcount GT 1><p><a href="/" title="Home">Home</a>
    <cfloop query="qBreadcrumbs">
        <cfoutput>
            <cfif title NEQ "index">&nbsp;&raquo;&nbsp;<a href="#url#" title="#title#">#title#</a></cfif>
        </cfoutput>
    </cfloop>
</p></cfif>
</div>
</cfoutput>


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes