In my quest for providing simpler topics about Sitecore development, I have decided to give you one of my simplest XSLT’s: the breadcrumb. This breadcrumb is probably used in every project I have ever implemented.
To create a breadcrumb you must read the current item, and all of it’s parents untill a you reach the front page and list them in the order from the front page and downwards to your own page. To do this, you must use the ancestor-or-self axis. This axis returns all descendants of your page and the page itself.
Here is the complete code. This is all you need:
<xsl:variable name="navigationItems" select="$sc_currentitem/ancestor-or-self::item[sc:IsItemOfType('mypages',.) and sc:fld('ShowInMenu',.)='1']"/>
<ul>
<xsl:for-each select="$navigationItems">
<li>
<xsl:if test="position()>1">
>
</xsl:if>
<sc:link text="{sc:clip(sc:fld('MenuTitle',.),20,1)}" />
</li>
</xsl:for-each></ul>
Here is how it works: First I use the ancestor-or-self axis to get all pages of a certain type (using sc:IsItemOfType(), see my blog on using this function) and with a certain property (using sc:fld(), see this blog on using sc:fld or sc:field).
The axis have done all the work for me. Now I can loop over each item and draw them in an unordered list. To seperate each of the items I draw a “>” but only if I am not drawing the first item (using position() > 1).
Notice how I draw the breadcrumb link. I use the sc:link() and clips the menu title (using sc:clip()) if the title is too long.
You should replace “mypages“, “ShowInMenu” and “MenuTitle” with the appropriate values you use in your project.
May 30, 2009 at 11:46 pm
Hey Great Work,
But Could you please tell me which value we have to replace
instead of these mypages“, “ShowInMenu” and “MenuTitle
Thanks for your time
June 3, 2009 at 8:11 am
The sc:IsItemOfType(‘mypages’,.) function returns all items in the axis that inherits from “mypages”. This is a way to filter all items that is not of a type that you do not wish to include in your breadcrumb. So here is a tip from Pentia’s agile Sitecore design (http://mcore.wordpress.com/2009/05/27/agile-sitecore-design/): Create a template with no fields on it. Name the template “WebPages”. Then let all of the pages that is a web page inherit from this. Then you can filter on this template in the menu, the breadcrumb etc. by adding the sc:IsItemOfType(‘WebPages’,.) caluse to the axis.
“ShowInMenu” is a field I have created on my template. It is a checkbox, that will allow me to hide items from my breadcrumb. If the checkbox is not checked, the item is not shown in the breadcrumb.
“MenuTitle” is also a field on my template. In this field I write the name of the item that I wish to have displayed in my breadcrumb, my menu etc. Why not use the ordinary page title? It is very common that menu titles are shorter than page titles.