Copied to clipboard

Flag this post as spam?

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


  • Morten 105 posts 345 karma points
    Feb 28, 2017 @ 09:53
    Morten
    0

    How to distinguish items with different content type on the same page

    Hi, everyone! So this is a two-part question. On my frontpage, I have two item lists I want to list: Services and news. Services are only going to be shown on the frontpage, whereas the news is limited to 4 on the frontpage, but there will be a news page, which will contain all the news listed (no limit).

    Question 1) I have created a Document Type called ListNews. ListNews has 3 properties/tabs: Title, Content, Image. I now go to Content -> Landing Page(frontpage) -> Create new item. Now I have an item (which is a news article). This new article is stuck in between all the other items (such as the services). Now the question is:

    On my frontpage, if I want to pull out the services, I would do something like this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var selection = CurrentPage.Children.Where("Visible");
    }
    @foreach (var item in selection)
    {
        if (item.ContentType.Alias == "listServices")
        {
            var img = Umbraco.Media(item.image);
            <a href="@item.link">
                <img src="@img.Url" />
                <p class="service-text">@item.title</p>
            </a>
        }
    }
    

    Basically I would pull out ALL items and check if their alias is equal to a service. If they are, put them on the page. This is extremely "bad", considering this also pulls out news articles as well. How do I pull out just the services?

    Now comes the actual "news page", which will contain all the news articles ever created. I obviously want to reuse the Document Type and the data from the frontpage, but I would then again have to check if it's a news article I'm pulling out (using item.ContentType.Alias). Again, that's bad practise and should be avoided.

    Question 2) How many contents should I have? I have a frontpage with services and news, but I also have a news page which shows all the news in a grid, where I can click on one of them to get redirected to read the actual news. Right now I just have a "Landing Page" which has all my services and all my news. My guess would be: Create a content for each page which has a Document Type.

    I'm still new to this new style of CMS, so maybe I'm mixing up a few things.

    Thanks for reading!

  • Dennis Adolfi 1082 posts 6449 karma points MVP 6x c-trib
    Mar 01, 2017 @ 09:25
    Dennis Adolfi
    0

    Hi Morten.

    Since you are using Dynamics (CurrentPage) you can instead of writing @CurrentPage.Children.Where("Visible") your write @CurrentPage.ListServices.Where("Visible").

    However it's a bit nicer to work with strongly typed models instead of dynamics, and not really hard to replace. In your case, you would replace the line where you make your selection with:

    @{
        var selection = Model.Content.Children.Where(x => x.DocumentTypeAlias == "listServices" && x.IsVisible());
    }
    

    This will make your selection stronly typed. That also means that it's children will be strongly typed, which means you cannot write @item.title, instead you'll need to use the GetPropertyValue() method. Here is an complete example of how your above code can be replaced from dynamics to strongly typed (Also including the "newsPage" you mentioned, however I'm guessing the doctype alias since I cant see you code, but you get the idea.).

        @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        var services = Model.Content.Children.Where(x => x.DocumentTypeAlias == "listServices" && x.IsVisible());
        var newsPages = Model.Content.Children.Where(x => x.DocumentTypeAlias == "newsPage" && x.IsVisible());
    }
    
    
    <!-- Loop out Services -->
    @foreach (var service in services)
    {
        var serviceImg = Umbraco.Media(service.GetPropertyValue<int>("image")); <!-- This probobly needs a nullcheck. -->
        <a href="@(service.GetPropertyValue<string>("link"))">
            <img src="@serviceImg.Url"/>
            <p class="service-text">@(service.GetPropertyValue<string>("title"))</p>
        </a>
    }
    
    <!-- Loop out News Pages -->
    @foreach (var newsPage in newsPages)
    {
    
        var newsPageImg = Umbraco.Media(newsPage.GetPropertyValue<int>("image")); <!-- This probobly needs a nullcheck. -->
        <a href="@(newsPage.GetPropertyValue<string>("link"))">
            <img src="@newsPageImg.Url" />
            <p class="news-text">@(newsPage.GetPropertyValue<string>("title"))</p>
        </a>
    }
    

    But hey!, use whatever feels compfortable to you. If you like the Dynamic way, keep using it until you feel ready to start converting your code to Strongly Typed. And when you are, I really recommend Daves post here: http://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/

    Have a great day and take care!

Please Sign in or register to post replies

Write your reply to:

Draft