Copied to clipboard

Flag this post as spam?

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


  • Sagar 74 posts 275 karma points
    Jul 08, 2016 @ 04:47
    Sagar
    0

    How to implement HTMLised sitemap

    any Idea?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 08, 2016 @ 07:04
    Alex Skrypnyk
    1

    Hi Sagar,

    Try to use code snippet:

    @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>
        }
    }
    

    Got code from Dennis Aaen answer -https://our.umbraco.org/forum/developers/razor/47368-Sitemap-HTML

    THanks

  • Sagar 74 posts 275 karma points
    Jul 08, 2016 @ 07:36
    Sagar
    0

    Not working,

    some error in the code. So it is hitting my custom 404 page . :(

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Jul 08, 2016 @ 07:28
    Dennis Aaen
    101

    Hi Sagar,

    The code that Alex have linked to from one of my old post uses the old dynamic node razor, which is deprecated.

    Here is an updated codesnippet that uses dynamic Razor.

    @{ var selection = CurrentPage.Site(); }
    
    <div class="sitemap">
        @* Render the sitemap by passing the root node to the traverse helper, below *@
        @Traverse(selection)
    </div>
    
    
    @* 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 selection = node.Children.Where("Visible").Where("Level <= " + maxLevelForSitemap);
    
        @* If any items are returned, render a list *@
        if (selection.Any())
        {
            <ul>
                @foreach (var item in selection)
                {
                    <li class="[email protected]">
                        <a href="@item.Url">@item.Name</a>
    
                        @* Run the traverse helper again for any child pages *@
                        @Traverse(item)
                    </li>
                }
            </ul>
        }
    }
    

    Hope this helps,

    /Dennis

  • Sagar 74 posts 275 karma points
    Jul 08, 2016 @ 08:18
    Sagar
    0

    Thnxx Dennis, it is working perfectly. :)

Please Sign in or register to post replies

Write your reply to:

Draft