Copied to clipboard

Flag this post as spam?

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


  • Jedd France 29 posts 140 karma points
    Apr 03, 2015 @ 17:26
    Jedd France
    0

    Separate Newest Article From The Rest?

    Hello everyone,

    I am adding a news section to my website using Razor, which filters through the news articles i've created in umbraco and displays them. This is the code that I have just now (which works fine)

    @using umbraco.MacroEngines;
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
        DynamicNode currentPage = Model;
    }

    @foreach (DynamicNode newsArticle in currentPage.Descendants("NewsArticle"))
    {
       

           

    @(newsArticle.GetPropertyValue("Name"))


            
           

                @(DateTime.Parse(newsArticle.GetPropertyValue("articleDate")).ToString("MMM"))

                '@(DateTime.Parse(newsArticle.GetPropertyValue("articleDate")).ToString("yy"))

            @Html.Raw(newsArticle.GetPropertyValue("bodyText"))

    }

    What I want to do is be able to separate the newest article from the rest so I can style it differently. The rest of the news articles will have the same styling. I'm not sure if I would need to use the createdate or the articleDate property I have on the doctype to do this. Any help would be much appreciated.

    Thanks!

    Jedd

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 03, 2015 @ 17:54
    Jan Skovgaard
    0

    Hi Jedd

    I think that should be possible if you order your news items by either the articleDate or the createdDate - But I guess that the article data would make the most sense to use?

    So something like

    @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContex
    
    @{
    DynamicNode currentPage = Model;
    Int i = 0;
    }
    
    @foreach (DynamicNode newsArticle in currentPage.Descendants("NewsArticle").OrderBy("x.articleDate")){
          <div class="newsContent @(i == 0 ? "latestNews" : "")">
              <h3>@(newsArticle.GetPropertyValue("Name"))</h3>
              <div class="articleDate">
                 @(DateTime.Parse(newsArticle.GetPropertyValue("articleDate")).ToString("MMM"))
                  <span>'@(DateTime.Parse(newsArticle.GetPropertyValue("articleDate")).ToString("yy"))</span>
              </div>
              @Html.Raw(newsArticle.GetPropertyValue("bodyText"))
         </div>
        i++;
    }
    

    In this example I'm decklaring an integer (i) and assigning the value 0 to it and then in the foreach loop I'm checking if the value of "i" is 0 using a ternary operator. If the expression is true the "latestNews" class is added otherwise nothing is added.

    In the foearch expression I have add the "OrderBy("articleDate")" - This should order the news items using the article date.

    I hope this makes sense and helps - And you might be able to benefit from having a look at this Razor cheatsheet https://our.umbraco.org/projects/developer-tools/umbraco-v6-mvc-razor-cheatsheets

    Hope this helps.

    /Jan

  • Jedd France 29 posts 140 karma points
    Apr 08, 2015 @ 18:00
    Jedd France
    100

    Hi Jan,

    Thank you for your response. I tried implementing your theory but it showed a few errors.

    Not to worry though, I managed to achieve what I wanted by using the structure of my articles in Umbraco.

    Thanks again!

    Jedd

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Apr 08, 2015 @ 22:13
    Jan Skovgaard
    0

    Hi Jedd

    Good to hear you managed to find a way - Do you mind sharing the code? Others might benefit from this as well :)

    Cheers, Jan

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies