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> }
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)
{
@*...*@
}
}
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?
@{ @*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> } }
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:
Anyone got any pointers or examples? :)
/Michael
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.
Thanks, Dan.
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:
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:
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
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:
Thanks, Dan.
Hi Dan
I got it working. Here is my final code.
Thank you for your time and input Dan :)
/Michael
is working on a reply...