Copied to clipboard

Flag this post as spam?

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


  • Kate 267 posts 610 karma points
    Jan 09, 2014 @ 15:57
    Kate
    0

    Find first and last element in Sitemap

    Hi

    I have this Sitemap on my site and I would like to add a class to all the items at the first level. (Company, Product, Guide and Contact)

    The last item at the first level need yet another class (Contact)

    My navigation structur look like this:
    Company
         vision
         Job
    Product
    Guide
    Contact

    Her is the sitemap code I use:

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{
    @* Walk up the tree from the current page to get the root node *@
    var rootNode = Model.AncestorOrself(1);
    }

    @*Render the sitemap by passing the root node to the traverse helper*@
    <div class="sitemap">
    @traverse(@Model.AncestorOrSelf())
    </div>



    @*Helper method to travers through all descendants*@
    @helper traverse(dynamic node){

    @*If a MaxLevelForSitemap parameter is passed to the macro, otherwise default to 4 levels*@
    var maxLevelForSitemap = String.IsNullOrEmpty(Parameter.MaxLevelForSitemap) ? 4 : int.Parse(Parameter.MaxLevelForSitemap);

    @*Select visible children *@
    var items = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);


    @*If any items are returned, render a list *@
    if (items.Any()) {
    <ul>
    @foreach (var item in items) {
    <li class="[email protected]">
    <a href="@item.Url">@item.Name</a>

    @*Run the traverse helper again *@
    @traverse(item)
    </li>
    }
    </ul>
    }
    }

    Hope you can help me :-)

    /Kate

  • Fuji Kusaka 2203 posts 4220 karma points
    Jan 09, 2014 @ 19:00
    Fuji Kusaka
    1

    Hi Kate,

    Change this 

     <li class="[email protected]">
                     <a href="@item.Url">@item.Name</
    a>

                 
    @*Run the traverse helper again *@
                     
    @traverse(item)
                   
    </li>
                }

    to

    var t = item.IsLast()? "class=last" : "class=first";
    <li @t>
    <a href="@item.Url">@item.Name</a>
     @*Run the traverse helper again *@
    @traverse(item)
    </li>
  • Kate 267 posts 610 karma points
    Jan 13, 2014 @ 10:09
    Kate
    0

    Hi FUji

    Sorry my late reply.
    And
    that is exactly what I needed :-)

    Thanks for your help

    Just another quistion. Is there any way I can keep [email protected] in the class?

    /Kate

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Jan 13, 2014 @ 10:30
    Fuji Kusaka
    2

    Hi Kate,

    Yes you can keep both by doing something like

    var t = item.IsLast()?"last":"first";
    <li class="[email protected] @t">
    <a href="@item.Url">@item.Name</a>
     @*Run the traverse helper again *@
    @traverse(item)
    </
    li>
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 13, 2014 @ 10:36
    Dennis Aaen
    0

    Hi Kate,

    You can do something like this:

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{   
        @* Walk up the tree from the current page to get the root node *@
        var rootNode = Model.AncestorOrself(1);
    }

    @*Render the sitemap by passing the root node to the traverse helper*@
    <div class="sitemap">
        @traverse(@Model.AncestorOrSelf())
    </div>

    @*Helper method to travers through all descendants*@
    @helper traverse(dynamic node){

    @*If a MaxLevelForSitemap parameter is passed to the macro, otherwise default to 4 levels*@
    var maxLevelForSitemap = String.IsNullOrEmpty(Parameter.MaxLevelForSitemap) ? 4 : int.Parse(Parameter.MaxLevelForSitemap);

    @*Select visible children *@
    var items = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);

    @*If any items are returned, render a list *@

    if (items.Any()) {
       <ul>
                @foreach (var item in items) {
                    var t = item.IsLast()? "last" : "first";
              <li class="[email protected] @t">
                        <a href="@item.Url">@item.Name</a>

                 @*Run the traverse helper again *@
                        @traverse(item)
                    </li>
                }
       </ul>
        }
    }

    Fuji, you were faster than me

    /Dennis

  • Fuji Kusaka 2203 posts 4220 karma points
    Jan 13, 2014 @ 10:49
    Fuji Kusaka
    0

    Lol, thats alright @Dennis :)

  • Kate 267 posts 610 karma points
    Jan 13, 2014 @ 10:55
    Kate
    0

    Hi

    Thanks to both of you.

    It works perfectly :-)

    /Kate

     

Please Sign in or register to post replies

Write your reply to:

Draft