Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Aug 11, 2011 @ 13:37
    Ismail Mayat
    0

    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

    @using umbraco.MacroEngines
    @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);
    
        <div class="sixcol">
            <ul>
            @foreach (var item in Model.usefulLinks.BaseElement.Elements("link"))
            {
                if (item.Attribute("type").Value == "internal")
                {
                    <li><a href="@umbraco.library.NiceUrl(int.Parse(item.Attribute("link").Value))">@item.Attribute("title").Value</a></li>
                }
                else { 
                    <li><a href="@item.Attribute("link").Value" rel="external">@item.Attribute("title").Value</a></li>
                }                                                                                                                                                                                                                                                                                                                   
            }  
            </ul>
        </div>
    }

     

    what i want to do is for first loop take noPerPage then next column skip noPerPage and take again noPerPage

    Any ideas?

    Regards

     

    Ismail

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Aug 11, 2011 @ 13:40
    Sebastiaan Janssen
    1

    Use grouping (umbraco 4.7.1. nighlty dll only!), example: 

        foreach (var group in Model.Children.InGroupsOf(2))
        {
            <div class="column-holder">
                <div class="column-frame">
                @foreach (var item in group)
                {
                    <div class="column">
                        <div class="column-area">
                            <a href="@item.Url" class="link">
                                @item.Name
                            </a>
                            <div class="heading equal">
                                <h2>
                                    @item.Name
                                </h2>
                            </div>
                            <p class="minHeight">
                                @item.introText
                            </p>
                        </div>
                        <div class="links">
                            <a href="@item.Url" class="more">naar dossier</a>
                        </div>
                    </div>
                }
                </div>
            </div>
        }
  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Aug 11, 2011 @ 13:46
    Sebastiaan Janssen
    1

    Or, for alternating even and odd classes, have a look at: 
    @Library.IsEven and  @Library.IsOdd

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Aug 11, 2011 @ 14:00
    Sebastiaan Janssen
    1

    FYI, usage of IsEven for example:

    @foreach (var item in Model.Children) {
       <p class="@(item.IsEven("even", "odd"))">item.Name</p>

    Would output:

    <p class="odd">Name1</p>
    <p class="even">Name2</p>
    <p class="odd">Name3</p> 
  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Aug 11, 2011 @ 14:15
    Ismail Mayat
    0

    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

     

     

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 12, 2011 @ 10:59
    Dan Diplo
    0

    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();
  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Aug 12, 2011 @ 11:13
    Ismail Mayat
    0

    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

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 12, 2011 @ 11:27
    Dan Diplo
    0

    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.

  • Chris Randle 67 posts 181 karma points c-trib
    Oct 26, 2011 @ 16:46
    Chris Randle
    0

    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.

  • Thomas Morris 1 post 71 karma points c-trib
    Feb 12, 2013 @ 13:29
    Thomas Morris
    0

     

    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;
    <ul class="list-block">
    @foreach (var subItem in items.Skip(s).Take(4)) {
    <li><a href="@subItem.Url">@subItem.Name</a></li>
    }
    </ul
    >
    }


     

Please Sign in or register to post replies

Write your reply to:

Draft