Copied to clipboard

Flag this post as spam?

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


  • René Andersen 238 posts 684 karma points
    Feb 13, 2015 @ 11:33
    René Andersen
    0

    Sitemap with special layout

    Hi,

    I need a sitemap with a special layout. Right now I have no problems showing the sitemap, but the layout and code is wrong. The code looks like this:

    @Traverse(CurrentPage.AncestorOrSelf(1))
    @helper Traverse(dynamic node)
    {
    var maxLevelForSitemap = 4;
    var items = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);

    if (items.Any())
    {
    <div class="mb">
    <h2>About us</h2>
    <ul>
    @foreach (var item in items)
    {
    <li><a href="@item.Url">@item.Name</a></li>
    @Traverse(item)
    }
    </ul>
    </div>
    }
    }

    And the HTLM output looks like this:

    <div class="mb">
    <h2>About us</h2>
    <ul>
    <li><a href="#">Link 1</a></li>
    <div class="mb">
    <h2 class="no-mt">About us</h2>
    <ul>
    <li><a href="#">Link 2</a></li>
    <li><a href="#">Link 3</a></li>
    </ul>
    </div>
    <li><a href="#">Link 4</a></li>
    <li><a href="#">Link 5</a></li>
    <li><a href="#">Link 5</a></li>
    </ul>
    </div>

    I need it to look like this. Where the "h2" is the name of the Parent page and "li" is the children pages and then the "div class mb" is used around every group of links.

    <div class="mb">
    <h2>About us</h2>
    <ul>
    <li><a href="#">About us 1</a></li>
    <li><a href="#">About us 2</a></li>
    </ul>
    </div>
    <div class="mb">
    <h2>Team</h2>
    <ul>
    <li><a href="#">Team 1</a></li>
    <li><a href="#">Team 2</a></li>
    </ul>
    </div>

    Is it possible and what do I need to change in the code? Does it have something to do with the location of "@Traverse(item)"? I am thinking maybe use "In group of".

    Any suggestions?

    // René

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Feb 13, 2015 @ 11:59
    Alex Skrypnyk
    0

    Hi Rene,

    Can you try to use few for each nested each other ?

  • René Andersen 238 posts 684 karma points
    Feb 13, 2015 @ 14:32
    René Andersen
    0

    Hi Alex,

    I have tried to search for an example on "few foreach" but I only find php code.

    Can you post an code example?

    Thanks!

    // René

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Feb 13, 2015 @ 20:24
    Dennis Aaen
    100

    Hi René

    I have take a look at your code for the sitemap, and with this code you should get the HTML outputted, as you described in your post.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage


    @* Render the sitemap by passing the root node to the traverse helper *@

        @Traverse(CurrentPage.AncestorOrSelf(1))

    @* Helper method to travers through all descendants *@
    @helper Traverse(dynamic node)
    {
        @* Update the level to reflect how deep you want the sitemap to go *@
        var maxLevelForSitemap = 4;
       
        @* Select visible children *@
        var items = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);

        @* If any items are returned, render a list *@
        if (items.Any())
        {
           
            foreach(var item in items.Where("Visible").Where("Level == 2")){
                <div class="mb">
                    <h2>
                        @item.Name
                    </h2>
                        <ul>
                            @foreach(var sitemapItem in item.Children){
                               
                                    <li class="[email protected]">
                                        <a href="@sitemapItem.Url">@sitemapItem.Name</a>
           
                                        @* Run the traverse helper again *@
                                            @Traverse(sitemapItem)
                                    </li>
                               
                            }
                    </ul>
                </div>
            }
           
          
        }
    }

    Maybe you need to do some modification depending on how your content structure looks like.

    Hope this helps,

    /Dennis

  • René Andersen 238 posts 684 karma points
    Feb 16, 2015 @ 10:15
    René Andersen
    0

    Hi Dennis,

    It works perfectly. Thanks!

    // René

Please Sign in or register to post replies

Write your reply to:

Draft