Copied to clipboard

Flag this post as spam?

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


  • Nikki Strømsnes 16 posts 58 karma points
    Nov 05, 2014 @ 12:14
    Nikki Strømsnes
    0

    Show specific children from two levels

    I'm in a bit of a sticky situation.

    I have a page, which may include several content modules, which should be output on the page. The page might also have subpages – these should of course not be output on the page, but in a menu somewhere. To make it easier for the content provider, the modules can be grouped together in an optional "Modules" folder under the page. The structure is thus:

    – My Page
    ––– Content 1
    ––– Modules
    ––– ––– Content 2
    ––– Subpage
    ––– ––– Content 3

    The page "My Page" should thus list "Content 1" and "Content 2", while ignoring "Subpage" and its decendants.

    This is done simply by creating two lists (CurrentPage.Children and CurrentPage.Modules.Children) and then concatenating them, as such:

    // Retrieve all modules ... except pages
    var modulesInFolder = CurrentPage.Modules.First().Children.Where("NodeTypeAlias != \"Standard\"").OrderBy("sortOrder");
    var modulesOnPage = CurrentPage.Children.Where("NodeTypeAlias != \"Standard\"").OrderBy("sortOrder");
    // Concatenate the two lists, so we can loop through them
    var moduleList = modulesInFolder.Concat(modulesOnPage).ToList();

    This works great – however, the "Modules" folder is optional. When it doesn't exist, nothing is output on the page at all. Presumably because "modulesInFolder" can not be declared. If I switch the two, it tells me "modulesOnPage does not conain a definition for 'Concat'".

    I have tried a few different things, such as counting on the number of elements, and defining 'moduleList' differently epending on the result, but to no avail.

    How can I solve this more elegantly?

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 05, 2014 @ 13:39
    Dennis Aaen
    0

    Hi Nikki,

    I would try to help the best I can. Does the "Content 1" and "Content 2" and "Content3" always use the same document type, if so then you could use the Descendants to get all the "Content 1" and "Content 2" and so on.

    @{    
        @*Build a query and return the visible items *@
        var selection= CurrentPage.Descendants("umbNewsItem").Where("Visible");
       
    }
       <ul>
         @foreach(var page in selection){               
           <li><a href="@page.Url">@page.Name</a></li>
         }                
       </ul>            

    Remember to change the "umbNewsItem", so it match the document type alias of Content 1, Content 2 and so on.

    If it can have differnt document types, then you could in your where clause specify which document types it should take.

    @{    
        @*Build a query and return the visible items *@
        var selection= CurrentPage.Descendants().Where("DocumentTypeAlias == @0 || DocumentTypeAlias == @1", "umbTextPage", "umbNewsItem");
         
    }

     <ul>
       @foreach(var page in selection){               
         <li><a href="@page.Url">@page.Name</a></li>
       }                
     </ul>    

    Again remeber to change the document type aliases, so it match your setup.

    Hope this helps, or at least can inspire you to a solution.

    /Dennis

  • Nikki Strømsnes 16 posts 58 karma points
    Nov 05, 2014 @ 16:27
    Nikki Strømsnes
    0

    Well, it's not exactly what I want, since this will also show content from any subpages on the current page. It should only take the content that are direct children, or children to the "module folder" document type.

    However, it does give me more to work with in order to find a final solution. Thanks! :)

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Nov 05, 2014 @ 18:12
    Dennis Aaen
    0

    Hi Nikki,

    I think I found a way that you could do it. I assume that your module folder has a specific document type, if so try this.

    @{    
        @*Build a query and return the visible items *@
        var selection = CurrentPage.Children().Where("Visible");   
    }
    <ul>
        @foreach(var page in selection.Where("Visible")){               
            <li>
                <a href="@page.Url">@page.Name</a>
            </li>
        }  
        @{
            var modulFolder = CurrentPage.DescendantsOrSelf("Modules").FirstOrDefault();
        }

         @foreach(var page in modulFolder.Children.Where("Visible")){               
            <li>
                <a href="@page.Url">@page.Name</a>
            </li>
         }   
    </ul>

    The only limit is that you only can have one module folder.

    Hope this could be a solution for you.

    /Dennis

  • Nikki Strømsnes 16 posts 58 karma points
    Nov 11, 2014 @ 14:17
    Nikki Strømsnes
    0

    Hi Dennis,

    Sadly, this doesn't really solve my problem. Every page may or may not have a "Modules" folder with content, and they may or may not have content as direct child nodes.

    The problem is, that if the folder does not exist as a child node, no output is generated at all – presumably because we are trying to traverse a structure that is not there. This doesn't change if I create two loops, except that it will output an error instead of an empty loop.

     

     

Please Sign in or register to post replies

Write your reply to:

Draft