Copied to clipboard

Flag this post as spam?

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


  • Michael 24 posts 137 karma points
    Nov 17, 2014 @ 23:28
    Michael
    0

    Get X number of items based on macro parameter value

    Hi guys.

    I am working on a simple newsfeed. I have it working at the moment, but now i would like to extend it so it uses the number entered when you insert the macro. I have a macroparameter called maxItems on my macro. My code looks like this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{   
      var selection = CurrentPage.DescendantsOrSelf("Artikel").Where("Visible");
      var newsItems = Umbraco.Content(Model.MacroParameters["maxItems"]);
    }

    @*Determine if there are any nodes in the selection, then render list *@
    @if(selection.Any()){

      foreach(var page in selection.OrderBy("CreateDate desc").Take(2)){
        <p>@page.Name</p>
      }                             

    }

    Anyone got any pointers or examples? :)

     

    /Michael

  • Dan Lister 416 posts 1974 karma points c-trib
    Nov 18, 2014 @ 00:02
    Dan Lister
    0

    Hi Michael,

    If I've understood your question correctly, you could try something like the following. The below tries to convert your macro parameter to an integer. If succeeded and the integer is greater than 0, only take the max number of items will be taken. If failed or less than 1, all items will be taken.

    @{
        var maxItems = 0;
    
        var items = int.TryParse(Model.MacroParameters["maxItems"], out maxItems) && maxItems > 0
            ? selection.OrderBy("CreateDate desc").Take(maxItems) 
            : selection.OrderBy("CreateDate desc");
    
        foreach (var item in items)
        {
            @*...*@
        }
    }
    

    Thanks, Dan.

  • Michael 24 posts 137 karma points
    Nov 18, 2014 @ 00:51
    Michael
    0

    Hi Dan

    I can not get that to work. But it got me thinking. I played around with it and my code now looks like this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{   
      var selection = CurrentPage.DescendantsOrSelf("Artikel").Where("Visible");
      var newsItems = Umbraco.Content(Model.MacroParameters["maxItems"]);
      int number = newsItems;
      @*var items = int.TryParse(newsItems);
      var maxItems = 0;

        var items = int.TryParse(Model.MacroParameters["maxItems"], out maxItems) && maxItems > 0
            ? selection.OrderBy("CreateDate desc").Take(maxItems)
            : selection.OrderBy("CreateDate desc");*@

    }

    @*Determine if there are any nodes in the selection, then render list *@
    @if(selection.Any()){
      @*foreach (var item in items)*@
      foreach(var page in selection.OrderBy("CreateDate desc").Take(2))
      {
       
    @page.Name

        @number
      }                             
    }

    On my page the @number renders as 0. But I entered 2 as the number when I inserted it into my template. So when I change the .Take(2) to .Take(number) it of course renders 0 :(

    If I do this instead:

    int number = selection.Count();

    and still have .Take(number) it now renders my 3 news items. Any idea why it's not fetching the value from the macroparameter in the template?

     

    /Michael

  • Dan Lister 416 posts 1974 karma points c-trib
    Nov 18, 2014 @ 00:57
    Dan Lister
    1

    Hi Michael,

    Correct me if I'm wrong but why are you calling Umbraco.Content() and passing your parameter to it? Shouldn't it just be:

    var number = 0;
    int.TryParse(Model.MacroParameters["maxItems"], out number);
    

    Thanks, Dan.

  • Michael 24 posts 137 karma points
    Nov 29, 2014 @ 19:15
    Michael
    102

    Hi Dan

    I got it working. Here is my final code.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @{   
      @*Build a query and return the visible items *@
      var selection = CurrentPage.DescendantsOrSelf("Artikel").Where("Visible");
      var number = Model.MacroParameters["maxItems"];
      int nuOfItems = Convert.ToInt32(number);
    }

    @*Determine if there are any nodes in the selection, then render list *@
    @if(selection.Any()){
      foreach(var page in selection.OrderBy("CreateDate desc").Take(nuOfItems)){
        <div class="nItem">
          <div class="nImg">
            @if (page.HasValue("billede")){                                        
              var dynamicMediaItem = Umbraco.Media(page.billede);
              <img src="@dynamicMediaItem.GetCropUrl("umbracoFile", "NyhedsBillede")" alt="@dynamicMediaItem.Name" />
              }
          </div>
          <div class="nHeadline"><h3>@page.overskrift</h3></div>
          <div class="nBtnDate"><a href="@page.Url" class="eBtnMore">Læs mere</a><span>@page.CreateDate.ToString("dd/MM/yyyy")</span></div>
        </div>
      }
    }

    Thank you for your time and input Dan :)

    /Michael

Please Sign in or register to post replies

Write your reply to:

Draft