Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Anthony Candaele 1197 posts 2049 karma points
    Oct 24, 2011 @ 14:48
    Anthony Candaele
    0

    problem with Razor breadcrumb

    Hi,

    For a Razor breadcrumb, I'm using this code from the Razor Recipes tutorial:

    <ul id="breadcrumb" class="group">
        @foreach (var level in @Model.Ancestors().Where("Visible"))
        {
            <li><href="@level.Url">@level.Name</a&gt;</li>
        }
        <li>@Model.Name</li>
    </ul>


    The problem is that is starts building links one level too high.

    This Razor code renders the following breadcrumb:

    Site (NL) > Home

    While it should only show: Home

    Also, unlike the Razor Cookbook demo site my Text pages are not childnodes of the home node. So the breadcrumb rendered for the Text Pages is this

    Site (NL) > Arrangementen

    While it should be: Home > Arrangementen

    My content tree in the Umbraco Backoffice looks like this:

    Can someone help me with tweaking this Razor Breadcrumb code, so the breadcrumb is rendered correctly (that is to say, Home > Textpage)

    Thanks for your help,
    Anthony Candaele
    Belgium

  • Dirk De Grave 4537 posts 6006 karma points MVP 3x admin c-trib
    Oct 24, 2011 @ 14:53
    Dirk De Grave
    1

    Anthony,

    You just can't using your current setup, you'll always have to "hardcode" the Home link instead of the 'Site (NL)', unless you add a property so 'Site (NL)' (eg BreadcrumbURLName) will be used in your Razor file instead of .Name

     

    Cheers,

    /Dirk

  • Anthony Candaele 1197 posts 2049 karma points
    Oct 24, 2011 @ 15:04
    Anthony Candaele
    0

    Hi Dirk,

    I included the hard coded "home" link. My breadcrumb looks like this:

    Home > Site (Nl) >Arrangementen

    So now I only should find a way not to include the Site (NL) link in my breadcrumb. I guess I should do this by starting the for each loop one level down, but I don't know how to code this in Razor.

    Thanks for your help,

    Anthony

  • Dirk De Grave 4537 posts 6006 karma points MVP 3x admin c-trib
    Oct 24, 2011 @ 15:08
    Dirk De Grave
    1

     http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet is your friend.... Just use

    .Ancestors(int level)

    where level is the level to start from..

     

    Cheers,

    /Dirk

  • Anthony Candaele 1197 posts 2049 karma points
    Oct 24, 2011 @ 15:20
    Anthony Candaele
    0

    Thanks a lot, this works. I just had to define a condition so that the breadcrumb doesn't render the home link twice when on the home page:

    <ul id="breadcrumb" class="group">
        @if (@Model.Name != "Home")
        {
         <li><href="/">Home &gt;</a></li>
         }
        @foreach (var level in @Model.Ancestors("Home").Where("Visible"))    
        {
            <li><href="@level.Url">@level.Name</a&gt;</li>
        }
        <li>@Model.Name</li>
    </ul>

    Greetings,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Oct 24, 2011 @ 20:46
    Anthony Candaele
    0

    I still have a problem. I'm using a multilanguage website:

    So when on an a Dutch page the breadcrumb should loop through all children of the node Site (NL) and when on a French page the breadcrumb should loop through all children of Site (Fr).

    Currently my breadcrumb script looks like this:

    <ul id="breadcrumb" class="group">
        @if (@Model.Name != "Home" &@Model.Name != "Accueil")
        {
         <li><href="/">Home</a&gt;</li>
         }
         
         
         @foreach (var level in @Model.Ancestors().Where("Visible"))    
           {       
            <li><href="@level.Url">@level.Name</a&gt;</li>       
           }
             
        <li>@Model.Name</li>
    </ul>

    Is there a way to make the foreach loop start from the Site (NL) node when on a Dutch page, and start from Site (Fr) when on a French page?

    Thanks for your help,

    Anthony Candaele

  • djscorch 67 posts 106 karma points
    Nov 13, 2011 @ 19:11
    djscorch
    0

    Sorry to hijack, but where can we download the cookbook package?

  • Anthony Candaele 1197 posts 2049 karma points
    Nov 14, 2011 @ 09:31
    Anthony Candaele
    0

    If you go to the introduction video tutorial of razor recipes, you can download the razor recipes package by clicking the 'download source code' link (at the bottom)

    greetings,

    Anthony

  • Sebastiaan Janssen 4899 posts 14655 karma points MVP admin hq
    Nov 17, 2011 @ 19:51
    Sebastiaan Janssen
    1

    Use AncestorOrSelf(1) to start from the first level instead of from the "Content" root node, maybe Ancestors(1) also work, haven't used that one yet.

  • Anthony Candaele 1197 posts 2049 karma points
    Nov 18, 2011 @ 09:21
    Anthony Candaele
    0

    Hi Sebastiaan,

    Thanks for the advice, I solved the problem in the meantime.

    The problem that I had was showing the homenode in the correct language for each multilingual site. I have tackled this problem by using dictionary items:

    <ul id="breadcrumb" class="group">
      @if (Model.NodeTypeAlias != "Homepage")
      {
       var output umbraco.library.GetDictionaryItem("Homenode");
       <li><href="/">@output</a&gt;</li>
       }
      @foreach(var level in Model.Ancestors().Where("Visible"))
      {
         <li><href="@level.Url">@level.Name</a&gt;</li>
      }
      <li>@Model.Name</li>
    </ul>

    greetings,

    Anthony

  • Sebastiaan Janssen 4899 posts 14655 karma points MVP admin hq
    Nov 18, 2011 @ 09:55
    Sebastiaan Janssen
    1

    Yup, that's what I usually do, well actually, I just hardcode the name really, how often are you going to change "Home" anyway.. :)

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Apr 25, 2012 @ 09:51
    Fuji Kusaka
    0

    Did something a bite similar for my multilingual website and here is how my codes looks like

    <ul>

    @{
          
          var homeNode umbraco.library.GetDictionaryItem("English");
      
            foreach(var  level in Model.Ancestors().Where("Visible"))
            {
             
             if(level.NodeTypeAlias == "home")   {
                <li><href="@level.Url">@homeNode</a></li>
              }
             
             else{
                <li><href="@level.Url">@level.Name</a></li>
               }
             
             }        
          <li>@Model.Name</li>  
      }

    </ul>

    //fuji

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies