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 28, 2014 @ 16:29
    Steve
    0

    Foreach with multiple conditions?

    I would like a navigation that has 2 sets of navigation on the page. One "tab" styled navigation which will lead to pages that will show a side navigation with only the descendants of that current page listed. Right now my tabs lead to pages that show not only the current pages descendants, but the ancestors. Here is my current razor and a link to the page for refrence. http://edit-wwwprep.rose-hulman.edu/offices-and-services/global-programs/current-students.aspx

    @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>     
    }
  • Steve 472 posts 1216 karma points
    Apr 28, 2014 @ 19:53
    Steve
    0

    Here is an image of what I need my razor to render. Just the current page and the decendants of that page. Problem is if I change the first foreach to
    @foreach (var node in page.DescendantsOrSelf().Where("Visible")) the page will not load and IIS will crash. What should I be doing?

  • Steve 472 posts 1216 karma points
    Apr 28, 2014 @ 22:16
    Steve
    0

    I've got it to work, but I would still like to know if it's possible to do multiple conditions on a foreach.

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Apr 29, 2014 @ 15:08
    Steve Morgan
    0

    Forgive me if I'm totally out here - but if you have this helper method that called each page Descendants OR SELF then wouldn't it get stuck in an infinite loop?  Is that what you mean by IIS crashing? 

  • Steve 472 posts 1216 karma points
    Apr 29, 2014 @ 15:14
    Steve
    0

    Yes, that correct. It is an infinite loop that crashes IIS.

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Apr 29, 2014 @ 15:28
    Steve Morgan
    0

    So if you change:

    @foreach (var node in page.DescendantsOrSelf().Where("Visible")) 

    to

    @foreach (var node in page.Descendants().Where("Visible")) 

    it works? 

  • Steve 472 posts 1216 karma points
    Apr 29, 2014 @ 15:45
    Steve
    0

    I've tried this and it doesn't pull any nodes in.

  • Steve Morgan 1349 posts 4459 karma points c-trib
    Apr 29, 2014 @ 16:55
    Steve Morgan
    0

    Your sample code is a little involved for me to recreate but I think you're just looking for all pages from and including the current page between certain levels (so you can exclude your homepage and very low level subpages) - is that right?

    If so you should just be able to use this - there is no need for recursion?!  You'll need to change the start and finish levels to your parameters and modify the output (which is currently just the URLs) to match your formatted links and business rules but it should give you what you need... 

     

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    
    @{
        int StartLevel = 1;
        int FinishLevel = 3;
    
        var page = CurrentPage.Descendants();
    
        string conditionStr = "Visible && Level <= " + FinishLevel + " && Level >= " + StartLevel;
    
        foreach (var node in page.Where(conditionStr)){
            @node.Url; 
        }
    
    }
  • Steve 472 posts 1216 karma points
    Apr 29, 2014 @ 17:34
    Steve
    0

    I am using Webforms not Mvc.

Please Sign in or register to post replies

Write your reply to:

Draft