Copied to clipboard

Flag this post as spam?

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


  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 15, 2012 @ 09:48
    Fuji Kusaka
    0

    Displaying only 5 Images and the remaining x amount only url

    Hi Guys,

    Can someone point out how to Display the first 5 images from a Media Picker and the remaining "x" amount to get only the url.

    I know how to get this done in XSLT but how to achieve this in Razor? 

     @foreach(dynamic img in items){
                      <span> <a href="@img.Url" rel="prettyPhoto[@Model.NodeTypeAlias]"><img src="@(img.UmbracoFile.Replace(".jpg","_thumb.jpg"))" width="100" height="100"/></a></span>                                               }

    if i make use of items.Take(5) it will obviously display only 5 items and thus limiting my slideshow to 5 instead of "x" amount .

     

  • Sandro 45 posts 118 karma points c-trib
    Aug 15, 2012 @ 10:22
    Sandro
    0

    What should the output HTML look like?

  • Douglas Ludlow 210 posts 366 karma points
    Aug 15, 2012 @ 16:39
    Douglas Ludlow
    0

    Use Skip() after Take() and you should be fine:

    foreach (dynamic img in items.Take(5))
    {
    <span> <a href="@img.umbracoFile" rel="prettyPhoto[@Model.NodeTypeAlias]"><img src="@(img.umbracoFile.Replace(".jpg", "_thumb.jpg"))" alt="" width="100" height="100" /></a></span>
    }

    foreach (dynamic img in items.Skip(5))
    {
    <span><a href="@img.umbracoFile" rel="prettyPhoto[@Model.NodeTypeAlias]">@img.Name</a></span>
    }

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 16, 2012 @ 08:07
    Fuji Kusaka
    0

    Hi Douglas,

    Thanks for the help. It worked indeed. I was thinking may be this could be done like xslt instead of having do make 2 foreach loops.

    More of something like

    <choose>
    <when>
    </when>
    <otherwise>
    </otherwise>
    </choose> 

    //fuji

     

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 19, 2012 @ 20:53
    Bo Damgaard Mortensen
    0

    Hi Fuji,

    You could also use a good ol' for loop to avoid looping through the list two times:

    // Can't remember if the Count is a property or a method: Count();
    for(int i = 0; i < items.Count; i++)
    {
    dynamic img = items[i];
    <span>
    <a href="@img.UmbracoFile" rel="prettyPhoto[@Model.NodeTypeAlias]">
    @if(i <= 5)
    {
    <img src="@(img.umbracoFile.Replace(".jpg", "_thumb.jpg"))" alt="" width="100" height="100" />
    }
    </a>
    </span>

    Above example is untested.

    Another thing is, that I'm not sure if it's "legal" in W3C terms to have a <img /> tag inside of a <span> tag, but that's a-whole-nother story ;-)

    Let me know how this works.

    All the best,

    Bo

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 21, 2012 @ 07:17
    Fuji Kusaka
    0

    Hi Bo,

    Thanks for the reply and help. Unfortunately this didnt work for me. 

    I will keep it as such for the time being  and whenever i gave some spare time i will try to make use of @helper to achieve and get the same results.

     

    //fuji

  • Bo Damgaard Mortensen 719 posts 1207 karma points
    Aug 21, 2012 @ 20:46
    Bo Damgaard Mortensen
    0

    Hi Fuji,

    Hmm, alright, but out of curiousity: where did it fail in my code snippet?

Please Sign in or register to post replies

Write your reply to:

Draft