Copied to clipboard

Flag this post as spam?

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


  • Steve 472 posts 1216 karma points
    Apr 22, 2014 @ 21:58
    Steve
    0

    @helper For Dynamic Nested List Navigation

    I am trying to understand the @helper method for creating a nested list navigation. I need to assign a class to the current page to style it. Could someone look at my razor and give me some suggestions?

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
    var page = Model.AncestorOrSelf(3).Children.Where("Visible");
        if(page != null)
        {
        @traverse(page);
        }                
    }   
    
    @helper traverse(dynamic page)
    {
        <ul class="academics-items">
            @foreach (var node in page.Children.Where("Visible"))
                {
                var childActive = node.IsEqual(Model) ? "class=activeChild" : "";
                var target = node.externalUrl ? "target=_blank" : "";
                var link = node.HasValue("externalUrl") ? node.url : node.Url;
                <li @childActive ><a href="@link" @target>@node.Name</a>
        @if(node.Children.Where("Visible").Count() > 0 && node.IsAncestorOrSelf(Model))
              { 
              @traverse(node);
              }
                </li>
              }
        </ul>     
    }
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 23, 2014 @ 08:43
    Jeavon Leopold
    0

    Hi Steve,

    Is this not working?

    var childActive = node.IsEqual(Model) ? "class=activeChild" : "";
    

    Jeavon

  • Steve 472 posts 1216 karma points
    Apr 23, 2014 @ 14:27
    Steve
    0

    The script doesn't load on the page, but it saves ok. So, there is something wrong with the way I've written it. Any ideas?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 23, 2014 @ 14:30
    Jeavon Leopold
    0

    So it shows a error?

  • Steve 472 posts 1216 karma points
    Apr 23, 2014 @ 14:33
    Steve
    0

    Error loading MacroEngine script (file: Category2LeftNav-RazorH.cshtml)

    Here is the page: https://edit-wwwprep.rose-hulman.edu/offices-and-services/insurance-risk-management/student-insurance.aspx


  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 23, 2014 @ 14:36
    Jeavon Leopold
    0

    Ok, try a catch or use Umbraco Debug Mode to see what the error is

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
        var page = Model.AncestorOrSelf(3).Children.Where("Visible");
        if (page != null)
        {
            @traverse(page);
        }
    }
    
    @helper traverse(dynamic page)
    {
        <ul class="academics-items">
            @{
                try
                {
                    foreach (var node in page.Children.Where("Visible"))
                        {
                            var childActive = node.IsEqual(Model) ? "class=activeChild" : "";
                            var target = node.externalUrl ? "target=_blank" : "";
                            var link = node.HasValue("externalUrl") ? node.url : node.Url;
                            <li @childActive>
                                <a href="@link" @target>@node.Name</a>
                                @if (node.Children.Where("Visible").Count() > 0 && node.IsAncestorOrSelf(Model))
                                {
                                    @traverse(node);
                                }
                            </li>
                        }
                }
                catch (Exception ex)
                {
                    <div>@ex.Message</div>
                }
            }
    
        </ul>
    }
    
  • Steve 472 posts 1216 karma points
    Apr 23, 2014 @ 14:42
    Steve
    0

    Okay here is the resulting message:

    'umbraco.MacroEngines.DynamicNodeList' does not contain a definition for 'Children'

  • Steve 472 posts 1216 karma points
    Apr 23, 2014 @ 16:00
    Steve
    0

    What is strange, we have another razor that uses a very similar helper running just fine. This is the other functioning helper. What is happening with my other script?

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{ 
    var startLevel = String.IsNullOrEmpty(Parameter.Level) ? 1 : int.Parse(Parameter.StartLevel);
    var finishLevel = String.IsNullOrEmpty(Parameter.Level) ? 3 : int.Parse(Parameter.FinishLevel);   
    var parent = @Model.AncestorOrSelf(startLevel);
    if (parent != null) { 
        <ul id="header-nav">
            @traverse(parent,startLevel,finishLevel)
        </ul>
    }
    }                                                     
    
    @helper traverse(dynamic parent,int startLevel,int finishLevel)
    {
    
        foreach (var node in parent.Children.Where("Visible")) {
            var selected = Array.IndexOf(Model.Path.Split(','), node.Id.ToString()) >= 0 ? " selected" : "";
            var hasChild = node.Children.Count() > 0 ? " with-submenu" : "";
            <[email protected](selected + hasChild)>
                <a href="@node.Url">@node.Name</a>                                       
                @if (node.Children.Count() > 0 && @node.Level<=finishLevel) { 
                    <ul>
                        @traverse(node,startLevel,finishLevel)
                    </ul>
                }  
            </li>
        }
    }
  • Steve 472 posts 1216 karma points
    Apr 23, 2014 @ 16:58
    Steve
    0

    Okay, I've gotten it to work with the following code, by removing the .Children.Where("Visible") from the variable page, but I don't want to add the "childActive" class to the top level list items. Is there a way to write this to only change the class on only the nested list item that are "current page"?

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
    var page = @Model.AncestorOrSelf(3);
        if(page != null)
        {
        @traverse(page);
        }                
    }   
    
    @helper traverse(dynamic page)
    {
        <ul class="academics-items">
            @foreach (var node in page.Children.Where("Visible"))
                {
                var childActive = node.IsEqual(Model) ? "class=activeChild" : "";
                var target = node.externalUrl ? "target=_blank" : "";
                var link = node.HasValue("externalUrl") ? node.url : node.Url;
                <li @childActive ><a href="@link" @target>@node.Name</a>
        @if(node.Children.Where("Visible").Count() > 0 && node.IsAncestorOrSelf(Model))
              { 
              @traverse(node);
              }
                </li>
              }
        </ul>     
    }
  • Steve 472 posts 1216 karma points
    Apr 23, 2014 @ 22:37
    Steve
    101

    Well, I figured it out myself. It was as simple as adding another ternary to the foreach.

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
    var page = @Model.AncestorOrSelf(3);
        if(page != null)
        {
        @traverse(page);    
        }                
    }   
    
    @helper traverse(dynamic page)
    {
        <ul class="academics-items">
            @foreach (var node in page.Children.Where("Visible"))
                {
            var levelOne = node.IsAncestor(Model) ? "class=levelOne" : "";
                var childActive = node.IsEqual(Model) ? "class=activeChild" : "";
                var target = node.externalUrl ? "target=_blank" : "";
                var link = node.HasValue("externalUrl") ? node.url : node.Url;
                <li @childActive @levelOne ><a href="@link" @target>@node.Name</a>
        @if(node.Children.Where("Visible").Count() > 0 && node.IsAncestorOrSelf(Model))
              { 
              @traverse(node);
              }
                </li>
              }
        </ul>     
    }
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 23, 2014 @ 23:01
    Jeavon Leopold
    0

    Well done! I was just about to come back to you :-)

  • Steve 472 posts 1216 karma points
    Apr 25, 2014 @ 16:14
    Steve
    0

    Jeavon,

    I am new to some of the newances of C# or Razor, and would like it if you could explain why I have to include "int" before my parameters for "StartLevel" and "FinishLevel" in my @helper traverse(dynamic page, int StartLevel, int FinishLevel) ?

    Aren't "StartLevel" and "FinishLevel", both parsed to integers when I assign the variables here?

    var StartLevel = String.IsNullOrEmpty(Parameter.startLevel) ? 1 : int.Parse(Parameter.startLevel);
    var FinishLevel = String.IsNullOrEmpty(Parameter.finishLevel) ? 3 : int.Parse(Parameter.finishLevel);

    Here is my full Razor with the helper:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @{
    var StartLevel = String.IsNullOrEmpty(Parameter.startLevel) ? 1 : int.Parse(Parameter.startLevel);
    var FinishLevel = String.IsNullOrEmpty(Parameter.finishLevel) ? 3 : int.Parse(Parameter.finishLevel);
    var page = @Model.AncestorOrSelf(StartLevel);
        if(page != null)
        {
        @traverse(page, StartLevel, FinishLevel);   
        }                
    }   
    
    @helper traverse(dynamic page, int StartLevel, int FinishLevel)
    {
        <ul class="nodeList">
            @foreach (var node in page.Children.Where("Visible"))
                {
                var itemName = node.HasValue("shortPageTitle") ? @node.shortPageTitle : node.HasValue("pageTitle") ? @node.pageTitle : @node.pageName;
                var levelOne = node.IsAncestor(Model) ? "class=levelOne" : "";
                var childActive = node.IsEqual(Model) ? "class=activeChild" : "";
                var target = node.externalUrl ? "target=_blank" : "";
                var link = node.HasValue("externalUrl") ? node.url : node.Url;
                <li @childActive @levelOne ><a href="@link" @target>@itemName</a>
        @if(node.Children.Where("Visible").Count() > 0 && node.IsAncestorOrSelf(Model) && node.Level <= FinishLevel)
              { 
              @traverse(node, StartLevel, FinishLevel);
              }
                </li>
              }
        </ul>     
    }
Please Sign in or register to post replies

Write your reply to:

Draft