Breadcrumbs are a pain in the ass in Confluence. Not just because there’s a default mode where Confluence inserts an ellipses (…), but also because the default Velocity template that draws the crumbs is overly complicated.
Plus, I am getting pretty tired of adding jQuery every time I want to customize something on my site: for example, I had a line in my Custom HTML that simulates a “click” event on the ellipses just so all the crumbs would appear.
Before, my Custom HTML included the following:
<!-- Automatically expand the "..." in the breadcrumbs when the page loads -->
AJS.$('#ellipsis strong').click();
<!-- Remove the redundant breadcrumb node -->
<!-- if there are exactly 3 breadcrumbs (for example: DevCenter > Documentation > Documentation), remove the third one (#2) to avoid redundancy -->
if (AJS.$('#breadcrumbs li').length == 3) {
AJS.$('#breadcrumbs li').eq(2).remove();
} else {
AJS.$('#breadcrumbs li').eq(3).remove();
}
What a PITA.
So, I decided to remove the jQuery calls, and simplify the Velocity template instead, customizing the crumbs to meet my site’s needs. Here’s my commented version of the breadcrumbs.vm file. This file is located in the /confluence directory.
Note: The file looks long, but half the lines are comments:
#requireResource("confluence.web.resources:breadcrumbs")
## breadcrumbs is a List of AbstractBreadcrumb objects
## http://docs.atlassian.com/atlassian-confluence/latest/com/atlassian/confluence/util/breadcrumbs/AbstractBreadcrumb.html
#set ($breadcrumbs = $helper.breadcrumbs)
<content tag="breadcrumbs">
#set ($counter = 0)
<ol id="breadcrumbs">
## iterate over each breadcrumb
#foreach( $breadcrumb in $breadcrumbs )
## skip the first two breadcrumbs (0 and 1) because they are redundant and unnecessary
#if ($counter > 1)
## if it's the second breadcrumb, then set the LI class to "first" so Confluence does not draw the separator
#if ($counter == 2) #set($crumbString = "first") #else #set($crumbString = "") #end
## get the displayTitle property to set the text of the crumb
#set ($displayTitle = $breadcrumb.displayTitle)
## draw the breadcrumb with the contentPath and target as the link, displayTitle as the text
<li class="$crumbString"><span><a href="$req.contextPath$breadcrumb.target">$displayTitle</a></span></li>
#else
## do nothing
#end ## end counter > 1
## increment the counter
#set($counter = $counter + 1)
#end ## end foreach
</ol>
</content>
Yeah, yeah, I know. It will break when I upgrade. It lacks much error checking. It doesn’t support tooltips. Whatever, it suits my needs.
To customize the separator, I just replace the images/decoration/white_breadcrumbs_indicator.png to an image that I want. I didn’t bother trying to track down where that image is set (I’m almost certain it’s hardcoded in the WAR file somewhere, and I just don’t have the heart to find it).