Copied to clipboard

Flag this post as spam?

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


  • René Andersen 238 posts 684 karma points
    Jan 30, 2015 @ 14:43
    René Andersen
    0

    Comma separated output

    Hi,

    The code below almost work but I need it to separate the output with a comma like this:

    /media/1005/test_image_645_1.jpg , /media/1006/test_image_645_2.jpg

    At the moment the output is without a comma like this:

    /media/1005/test_image_645_1.jpg /media/1006/test_image_645_2.jpg

    @if (childPage.HasValue("portfolioImages"))
    {
    var caseStudyImagesList = childPage.portfolioImages.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    var caseStudyImagesCollection = Umbraco.Media(caseStudyImagesList);

    foreach (var caseStudyImage in caseStudyImagesCollection)
    {
    @caseStudyImage.Url
    }
    }

    Does anyone have a simple solution?

    Thanks in advance!

    // René

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jan 30, 2015 @ 14:50
    Dennis Aaen
    2

    Hi Rene,

    With this code you should be good. :-)

    @if (childPage.HasValue("portfolioImages"))
    {
        var caseStudyImagesList = childPage.portfolioImages.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
        var caseStudyImagesCollection = Umbraco.Media(caseStudyImagesList);
       
        foreach (var caseStudyImage in caseStudyImagesCollection)
        {
          
            if(caseStudyImage.IsLast()){
                 @caseStudyImage.Url
            }else{
              @caseStudyImage.Url@:,
            }
               
        }
           
    }

    Hope this helps,

    /Dennis

  • Dan Lister 416 posts 1974 karma points c-trib
    Jan 30, 2015 @ 14:50
    Dan Lister
    101

    Hi René,

    You could try something like the following. It loops through each of the case study images, combining their urls together and then trimming the end comma:

    @if (childPage.HasValue("portfolioImages"))
    {
        var caseStudyImagesList = childPage.portfolioImages.Split(new [] { "," }, StringSplitOptions.RemoveEmptyEntries);
        var caseStudyImagesCollection = Umbraco.Media(caseStudyImagesList);
    
        var outputStr = string.Empty;
    
        foreach (var caseStudyImage in caseStudyImagesCollection)
        {
            outputStr += string.Format(" , {0}", caseStudyImage.Url);
        }
    
        @outputStr.TrimEnd(" , ")
    }
    

    Hope that helps.

    Thanks, Dan.

  • René Andersen 238 posts 684 karma points
    Jan 30, 2015 @ 15:00
    René Andersen
    2

    Hi Dennis and Dan

    Both solutions worked for me. Thank you!

    Have a nice weekend.

    // René

  • René Andersen 238 posts 684 karma points
    Jan 30, 2015 @ 15:10
    René Andersen
    0

    Hi Dan

    I just found one little problem regarding your solution. Because the output is beginning with a comma like below:

    ,/media/1005/test_image_645_1.jpg,/media/1006/test_image_645_2.jpg

    Is it possible to remove the comma in the beginning?

    Thanks!

    // René

  • Dan Lister 416 posts 1974 karma points c-trib
    Jan 30, 2015 @ 15:14
    Dan Lister
    0

    Ah of course. Trying changing your code to use the following line instead of TrimEnd():

    @outputStr.TrimStart(",")
    

    Thanks, Dan.

  • René Andersen 238 posts 684 karma points
    Jan 30, 2015 @ 15:18
    René Andersen
    0

    Ahh that was very simple...I am still learning thanks to guys like you. :-)

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft