Copied to clipboard

Flag this post as spam?

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


  • James 251 posts 1169 karma points
    Jul 19, 2014 @ 16:15
    James
    0

    Generating A partial View of items from another page

    Hello all,

    I understand how to generate a view of child items on a page.

     

    But how can i generate a view of  items on a page from another pages child nodes eg. On the Home page i want a list of 4 of the most recent portfolio items from inside:

    Portfolo

    • portfolioitem1
    • portfolioitem2
    • portfolioitem3
    Kind regards.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 19, 2014 @ 16:27
    Jeavon Leopold
    0

    Something like this on your homepage:

    @foreach (var page in CurrentPage.DescendantOrSelf("portfolioDocType").Children.Where("Visible"))
    

    Where "portfolioDocType" is the document type alias of your "Portfolio" page, hopefully it's unique and only used by that node.

  • James 251 posts 1169 karma points
    Jul 19, 2014 @ 16:48
    James
    0

    Thats very useful.

    Is there a way to select the first four?

    Or a specific number?

     

  • James 251 posts 1169 karma points
    Jul 19, 2014 @ 16:58
    James
    0

    The Portfolio Doc type i use is only use by that node and is called Portfolio.

    I used that exact code and it doesnt render anything when i pass HTML into the {} after the call.

     

    Is that the correct syntax?

     

     

  • James 251 posts 1169 karma points
    Jul 19, 2014 @ 17:53
    James
    0

    My content layout is as follows:

    Home

    About

    Portfolio

    • Portfolio item
    • portfolio item 2
    • portfolio item 3
    Contact
    When i use .DescendantOrSelf("Portfolio") on my home page. Will this find anything?
    Because Portfolio isnt a descendant. And it isnt itself.
    Kind regards.
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 19, 2014 @ 19:51
    Jeavon Leopold
    0

    Hi James,

    Ok, generally the standard approach is to have 1 root node per website (you can have multiple sites/languages) then all other pages are children of that root node. You will find all code snippets etc expect this structure.

    You can select only 4 items by using the .Take(n) method, e.g.

    @foreach (var page in CurrentPage.DescendantOrSelf("portfolioDocType").Children.Where("Visible").Take(4))
    

    You may want to add a OrderBy() in there before the .Take()

    Jeavon

  • James 251 posts 1169 karma points
    Jul 19, 2014 @ 20:12
    James
    0

    I now have all my content nodes inside the home content node.

     

    I have allowed child node types.

     

    I run that code exactly as it is written then with my "Portfolio" doc type and it isntpulling anything through.

     

    Any ideas?

     

  • James 251 posts 1169 karma points
    Jul 19, 2014 @ 20:30
    James
    0

    My documents types are all listed. And i have set my homepage doc type to allow all 3 of the child nodes.

     

    My content tab now looks like

     

    Home

    • About
    • Portfolio
    • Contact
    Using the exact code you have put but with an alias of "portfolio". I get no out put on my page :(.
    The code i am using:
    @foreach(var page in CurrentPage.DescendantOrSelf("portfolio").Children.Where("Visible").Take(4)){
                            <h3><a class="hover-effect" href="#">@page.Name</a></h3>
    }
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 19, 2014 @ 20:30
    Jeavon Leopold
    100

    Can you check the document type alias of Portfolio is exactly "portfolioDocType" and also try running this code on your homepage?

  • James 251 posts 1169 karma points
    Jul 19, 2014 @ 20:33
    James
    0

    I have double checked it and the Doc type "Portfolio" now has the alias of "portfolioDocType" My code references that too.

     

    This code is located on the home template undertneath my master.

     

    Still not outputting anything .

     

     

    Kind regards.

  • James 251 posts 1169 karma points
    Jul 19, 2014 @ 21:09
    James
    0

    I cant see any syntax errors in visual studio either.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 19, 2014 @ 21:29
    Jeavon Leopold
    0

    Ok, then this should be DescendantOrSelf("portfolioDocType") ?

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 19, 2014 @ 21:33
    Jeavon Leopold
    0

    Hmm, seems it doesn't work fully dynamic.

    This should work ok:

    @foreach (var page in CurrentPage.DescendantsOrSelf("umbNewsOverview").First().Children.Where("Visible").Take(4))
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 19, 2014 @ 21:39
    Jeavon Leopold
    0

    Or bizarrely this also works...

    @{
        DynamicPublishedContent currentPage = CurrentPage;
        foreach (dynamic page in currentPage.DescendantOrSelf("umbNewsOverview").Children.Where("Visible").Take(4))
        {
            <p>@page.title</p>
        }
    }
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Jul 21, 2014 @ 11:16
    Jeavon Leopold
    0

    Hi James,

    Did you get this working?

    Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft