ps-team
10/27/2017 - 9:56 AM

Razor breadcrumbs that work with both legacy content pages AND entries - requires customUtilities.cshtml (another gist)

Razor breadcrumbs that work with both legacy content pages AND entries - requires customUtilities.cshtml (another gist)

@using Contensis.Framework.Web;
@using System.Web;
@{

    // check if this is an entry
    var entryUrl = "";
    var entryTitle = "";
  	if (HttpContext.Current.Items["entryUrl"] != null)
    {
        entryUrl = HttpContext.Current.Items["entryUrl"].ToString();
      	entryTitle = HttpContext.Current.Items["entryTitle"].ToString();
    }
}


@if(!String.IsNullOrEmpty(entryUrl)) {
  
  
	string[] crumbs = entryUrl.Split('/');


	string pathbuilder = "/";

  
    <nav aria-label="You are here:" class="row column large-12 breadcrumbs">   
      <ul>
          @foreach (var crumb in crumbs.Skip(1)) {
              // makey makey URL
              pathbuilder = pathbuilder + crumb + "/";

              if (pathbuilder != entryUrl+"/") {

                	var formatFix = stringFunctions.FirstLetterToUpper(crumb).Replace("-"," ");
                	var dateFix = linkText(formatFix);
                
                  <li> <a href="@(pathbuilder)">@dateFix</a></li>
              }
              else {

                <li>@entryTitle</li>
              }

          }
      </ul>
    </nav>   

}
else {
  
  <nav aria-label="You are here:" class="row column large-12 breadcrumbs">   
    <ul>
      @breadcrumb(CurrentNode)    
    </ul>
  </nav>   
  @helper breadcrumb(Node node)
  {
     <!-- node depth,@node.Path == @node.Depth -->
     if(node.Depth>0)
     {
         var parent = node.Parent;
         if (node is ContentNode)
         {
             ContentNode contentNode = (ContentNode) node;
             if(contentNode.IsHomePage)
             {
                 parent=parent.Parent;
             }
         }
         @breadcrumb(parent)  
     }
     <li>
             @if(node==CurrentNode)
             {
                 @node.MenuName
             }   
             else
             {
               	var monthFix = linkText(node.MenuName.ToString());
       			<a href="@node.Path">@monthFix</a>
             }
  </li>    

  }
}
 
  
@functions {
    public string linkText(string date)
    {
        switch (date)
        {
            case "01":
          		return "January";
            case "02":
          		return "February";
            case "03":
                return "March";
            case "04":
          		return "April";
            case "05":
                return "May";
            case "06":
          		return "June";
            case "07":
          		return "July";
          	case "08":
          		return "August";
          	case "09":
          		return "September";
          	case "10":
          		return "October";
          	case "11":
          		return "November";
          	case "12":
                return "December";
            default:
                return date;
        }
    }

 
}