I have related links datatype i can loop through and display items no problem. What i want to do is display the items in 2 cols of ul li lists. The issue is I cannot do take and skip. This is what i have so far
Does that code work OK for you, Ismail? Whilst looking at your problem I tried it (against Umbraco 4.7 using standard RelatedLinks datatype) and always got this error:
'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'Count'
I'm curious how this can work for you and not me? As far as I can tell the related links are returned as the type umbraco.MacroEngines.DynamicXml
To get a count I thought you'd have to cast the dynamic type back to DynamicXml and then access the properties via BaseElement - something like this:
umbraco.MacroEngines.DynamicXml xml = Model.usefulLinks;
int count = xml.BaseElement.Descendants().Count();
For reference, you can get a count like I indicated above if you are not using 4.7.1 marcoengines assembly. But it's good to know it has been simplified in the latest release.
Also - you probably know this but - trying to open external links using just the rel attribute does not work. Instead, add the target attribute and set it's value to "_blank" to open in a new tab or window. Thanks.
Here's a little trick I used to split each list block into 4 items whilst still maintaining position. In this case, it was used for a block nav.
for (int i = 0; i items.Count() / 4; i++) { int s = i * 4; <ulclass="list-block"> @foreach (var subItem in items.Skip(s).Take(4)) { <li><ahref="@subItem.Url">@subItem.Name</a></li> } </ul> }
Take on related links
Guys,
I have related links datatype i can loop through and display items no problem. What i want to do is display the items in 2 cols of ul li lists. The issue is I cannot do take and skip. This is what i have so far
what i want to do is for first loop take noPerPage then next column skip noPerPage and take again noPerPage
Any ideas?
Regards
Ismail
Use grouping (umbraco 4.7.1. nighlty dll only!), example:
foreach (var group in Model.Children.InGroupsOf(2))
Or, for alternating even and odd classes, have a look at:
@Library.IsEven and @Library.IsOdd
FYI, usage of IsEven for example:
Would output:
Okay here is how i have done it probably not the best way of or cleanest way but it works
@using System.Xml.Linq
@using umbraco.presentation.nodeFactory
@inherits umbraco.MacroEngines.DynamicNodeContext
@if (Model.usefulLinks.ToString() != string.Empty)
{
int count = Model.usefulLinks.Count();
int noPerPage = (int)Math.Ceiling((decimal)count / 2);
int counter = 0;
int lastCounter=0;
<div class="sixcol">
<ul>
@foreach (var item in Model.usefulLinks)
{
if (item.IsNotPosition(noPerPage))
{
if (item.type == "internal")
{
<li><a href="@umbraco.library.NiceUrl(int.Parse(item.link))">@item.title</a></li>
}
else
{
<li><a href="@item.link" rel="external">@item.title</a></li>
}
}
else {
break;
}
counter++;
}
</ul>
</div>
if (count > noPerPage) {
<div class="sixcol last">
<ul>
@foreach (var item in Model.usefulLinks)
{
if (lastCounter >= counter && item.IsNotPosition(count))
{
if (item.type == "internal")
{
<li><a href="@umbraco.library.NiceUrl(int.Parse(item.link))">@item.title</a></li>
}
else
{
<li><a href="@item.link" rel="external">@item.title</a></li>
}
}
lastCounter++;
}
</ul>
</div>
}
}
Regards
Ismail
Does that code work OK for you, Ismail? Whilst looking at your problem I tried it (against Umbraco 4.7 using standard RelatedLinks datatype) and always got this error:
'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'Count'
I'm curious how this can work for you and not me? As far as I can tell the related links are returned as the type umbraco.MacroEngines.DynamicXml
To get a count I thought you'd have to cast the dynamic type back to DynamicXml and then access the properties via BaseElement - something like this:
Dan,
It all works I am also using 4.7 but macroengines dll 4.7.1 latest nightly http://nightly.umbraco.org/umbraco%204.7/4.7%20RC/4.7.1.419.zip its that dll that has those methods.
Regards
Ismail
Ahh, that explains it, Ismail. Thanks!
For reference, you can get a count like I indicated above if you are not using 4.7.1 marcoengines assembly. But it's good to know it has been simplified in the latest release.
Also - you probably know this but - trying to open external links using just the rel attribute does not work. Instead, add the target attribute and set it's value to "_blank" to open in a new tab or window. Thanks.
Here's a little trick I used to split each list block into 4 items whilst still maintaining position. In this case, it was used for a block nav.
is working on a reply...