Copied to clipboard

Flag this post as spam?

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


  • Tom 161 posts 322 karma points
    Feb 13, 2015 @ 13:30
    Tom
    0

    News Example

    We created a content type named NewsArticles) that has our required fields (Title, SubTitle, Description and Date).
    Note: When we created the content type (NewsArticles) we also created the matching template.

    In our site we have 5 places where we have created content based on NewsArticles.

    I now need some generic Razor logic to loop through each site's NewsArticles and display on a screen.
    I also need to know how to convert my template to a partial view.

    As I am new to Razor and Umbraco, can somehow point me in the right direction?

     

    Thanks


    Tom

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Feb 13, 2015 @ 21:01
    Dennis Aaen
    0

    Hi Tom,

    You need to use your news items 5 differeent places on your website, then you can create a partial view or a patial view macro and inserted it on the template where the news items should appears.

    I have made some razor code that should point you in the right direction, depeding on how your content / site structure is. But here is the code that you should you for getting news items that you can reuse on the your site.

    @{
        @* Get the root of the website *@
        var root = CurrentPage.AncestorOrSelf(1).Descendants("umbNewsOverview").FirstOrDefault();
    }

    <ul>
        @foreach (var page in root.Children.Where("Visible"))
        {
            <li class="@(page.IsAncestorOrSelf(CurrentPage) ? "current" : null)">
                <a href="@page.Url">@page.Name</a>
            </li>
        }
    </ul>

    I will try to explain what the code does, it will go up to the first level on your site, and then search the tree structure to find the first item with the document type alias of umbNewsOverview, and the pull out all it´s children. So depeding on your structure, then you need to change the umbNewsOverview, so it match with your alias for the document type that you are using for your overview page for your news section.

    If you are using Umbraco 7.2, then you have a query builder that can help you buliding your Razor code. When you are in a partial view, parial view macro or your in template, then press the button at the right on the ribbon. If you use the query builder the code would look something like this.

    @{
        var root = CurrentPage.Site().FirstChild("umbNewsOverview");
       
        foreach(var page in root.Children.Where("Visible")){
            <li class="@(page.IsAncestorOrSelf(CurrentPage) ? "current" : null)">
                <a href="@page.Url">@page.Name</a>
            </li>
        }
    }

    If you have access to Umbraco TV, there is two chapters about Razor http://umbraco.tv/videos/implementor/working-with-umbraco-data/razor-syntax/introduction-to-razor/ and http://umbraco.tv/videos/implementor/working-with-umbraco-data/querying-umbraco-data-with-razor/

    Here is also some documentation on Razor https://our.umbraco.org/documentation/Using-Umbraco/Creating-Basic-Site/Articles-Parent-and-Article-Items

    Hope this make sense and helps. If you have any questions to this don't hesitate to ask again.

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft