Copied to clipboard

Flag this post as spam?

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


  • Lex Godthelp 24 posts 106 karma points
    Jul 14, 2015 @ 09:58
    Lex Godthelp
    0

    Traversing menu - adding Home

    Hi guys,

    Quick question: I made a menu by using Traversing. Now i want to add a @if statement to add Home to the first level (nodelevel 2) of children.

    I have this code but i want to know where i go wrong:

    @using  Our.Umbraco.Vorto.Extensions
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{ var selection = CurrentPage.Site(); }
    @Traverse(selection)
    @helper Traverse(dynamic node){
        var maxLevelForSitemap = 4;
        var selection = node.Children.Where("menuweergave == true").Where("Level <= " + maxLevelForSitemap);
        if (selection.Any())
        {
            <ul>
                @if(selection.Level == 2){
                    <li class="menu_li_2"><a href="/">Home</a>  
                }
                @foreach (var item in selection.Where("menuweergave == true")){
                IPublishedContent sitem= (IPublishedContent)item;
                <li class="[email protected]"><a href="@item.Url">@sitem.GetVortoValue("paginatitel")</a>
                     @Traverse(item)
                </li>
                }
            </ul>
        }
    }
    

    Something goes wrong with selection.Level but i can't figure out what.

  • Gary Devenay 39 posts 245 karma points
    Jul 14, 2015 @ 12:06
    Gary Devenay
    0

    Hi Lex,

    It seems like the issue would be that you are not closing the <li> element at the end of your Home link. This will break your @if statement and cause your helper to fail.

    Try adding the </li> to the end of your link and see if that helps.

    Gary

  • Lex Godthelp 24 posts 106 karma points
    Jul 14, 2015 @ 12:08
    Lex Godthelp
    0

    That must have been a mistake whilst 'Ctrl + Y'-ing. Fixed it but still gives an error.

  • Gary Devenay 39 posts 245 karma points
    Jul 14, 2015 @ 12:09
    Gary Devenay
    0

    Would you mind sharing the error that you are receiving? This will help me debug.

  • Lex Godthelp 24 posts 106 karma points
    Jul 14, 2015 @ 12:11
    Lex Godthelp
    0

    I don't think it would help much but i get a Error loading Partial View script (file: ~/Views/MacroPartials/Menu.cshtml).

    Is there any way to get a function that checks your PVMF on error's like for the XSLT-files?

    Edit: Thanks for your help btw

  • Gary Devenay 39 posts 245 karma points
    Jul 14, 2015 @ 12:28
    Gary Devenay
    0

    Hi Lex,

    I just noticed that you are using selection.Level where selection is of type IEnumerable<IPublishedContent>.

    Try moving your @if statement inside your foreach loop and change selection.Level to item.Level

    Hopefully that solves your issue!

    Gary

  • Lex Godthelp 24 posts 106 karma points
    Jul 14, 2015 @ 12:44
    Lex Godthelp
    100

    Thanks for your response Gary, you helped me figure it out:

    @using  Our.Umbraco.Vorto.Extensions
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{ var selection = CurrentPage.Site(); }
    @Traverse(selection)
    @helper Traverse(dynamic node){
        var maxLevelForSitemap = 4;
        var selection = node.Children.Where("menuweergave == true").Where("Level <= " + maxLevelForSitemap);
        var selectionLevel = node.Level;
        if (selection.Any())
        {
            <ul>
                @if(selectionLevel == 1){
                    <li class="menu_li_2"><a href="/">Home</a></li>
                }
                @foreach (var item in selection.Where("menuweergave == true")){
                IPublishedContent sitem= (IPublishedContent)item;
                <li class="[email protected]"><a href="@item.Url">@sitem.GetVortoValue("paginatitel")</a>
                     @Traverse(item)
                </li>
                }
            </ul>
        }
    }
    

    I added another variable called selectionLevel to get the node.Levelvar selectionLevel = node.Level;. Then i addd this if statement:

            @if(selectionLevel == 1){
                <li class="menu_li_2"><a href="/">Home</a></li>
            }
    

    I couldnt use your suggestion because it would add another <li>home</li> to each var item. But it helped me realise what i was doing wrong, thanks!

Please Sign in or register to post replies

Write your reply to:

Draft