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
    Mar 16, 2015 @ 16:16
    René Andersen
    0

    Remove space in generated HTML

    Hi

    I use this code:

    @if (CurrentPage.HasValue("images"))
    {
    var imagesList = CurrentPage.images.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    var imagesCollection = Umbraco.Media(imagesList);

    foreach (var image in imagesCollection)
    {
    // first item and set css
    var itemClass = image.IsFirst("active");

    <div class="item @itemClass">
    <img src="@image.Url" class="img-responsive" alt="..." />
    </div>
    }
    }

    And it generates this HTML output:

    <div class="item active">
    <img src="/media/1003/slide_3.jpg" class="img-responsive" alt="..." />
    </div>
    <div class="item ">
    <img src="/media/1002/slide_2.jpg" class="img-responsive" alt="..." />
    </div>

    How is it possible to remove the space after "item" in the non-active image, see code below:

    <div class="item ">

    Thanks in advance!

    // René

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 16, 2015 @ 16:28
    Dennis Aaen
    1

    Hi René

    I just have a similar issue, and I solved it like doing this.

    Perhaps there is a better way of doing it, but this works for me.

    @if (CurrentPage.HasValue("images"))
    {
      var imagesList = CurrentPage.images.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
      var imagesCollection = Umbraco.Media(imagesList);

      foreach (var image in imagesCollection)
      {
        // first item and set css
            var itemClass = "";

            if(image.IsFirst()){
                itemClass = "active";
            }else{
                itemClass = null;
            }

        <div class="item @itemClass">
          <img src="@image.Url" class="img-responsive" alt="..." />
        </div>
      }
    }

    Hope this helps,

    /Dennis

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 16, 2015 @ 16:31
    Jan Skovgaard
    2

    Hi René

    You should be able to use a ternary operator for this...

    So you could make a check like this

    <div class="@image.IsFirst ? 'item active' : 'item'">
    

    Does this work?

    /Jan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 16, 2015 @ 16:32
  • René Andersen 238 posts 684 karma points
    Mar 16, 2015 @ 17:09
    René Andersen
    0

    Hi Dennis and Jan

    Dennis: Your solution works for me. Thanks

    Jan: I used this code:

    <div class="@(image.IsFirst ? "item active" : "item")">

    But it only uses the last option "item". It does not insert the "item active" on the first item.

    I would really like this to work with your solution, because it is a very clean solution.

    Any suggestions?

    // René

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 16, 2015 @ 17:22
    Jan Skovgaard
    1

    Hi René

    Hmm, ok...that's because image.IsFirst never returns true...ok...this can probably be done prettier but you should be able to do something like

    @{
    var isFirst = image.IsFirst("true");
    }
    
    <div class="@(isFirst == 'true' ? 'item active' : 'item')">
    

    Does this work?

    /Jan

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 16, 2015 @ 18:07
    Dennis Aaen
    100

    Hi René,

    Since you always want your div to have the class="item" and it´s only the addon class active that change. Then I think the most clean way you can do it is like this.

    <div class="item @(image.IsFirst() ? "active" : null)">

    Hope this helps,

    /Dennis

  • René Andersen 238 posts 684 karma points
    Mar 17, 2015 @ 08:06
    René Andersen
    0

    Hi Jan and Dennis

    Jan: I did not manage to get your solution to work, but you definitely got me and Dennis in the right direction. :-)

    I got this error message:

    Operator '==' cannot be applied to operands of type 'System.Web.HtmlString' and 'string'

    Dennis: Your solution works great.

    Thank you both of you. :-)

    // René

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Mar 17, 2015 @ 08:08
    Jan Skovgaard
    0

    Hi René

    Good that you managed to get it working - That's what matters. I was just trying to see if you could assign a string of true and then compare it. There should probably have been .ToString() in that sentence. But nevermind, using the (image.IsFirst) does the job :)

    /Jan

  • René Andersen 238 posts 684 karma points
    Mar 17, 2015 @ 09:03
    René Andersen
    0

    Hi Jan

    If you don't mind, then I would like to see how you would insert the .ToString() in your solution, maybe your solution could be used in another project. :-)

    I ran into another issue regarding this topic.

    My problem is that I want to add class="active" to the active item and nothing to the other items.

    I use this code and it almost works:

    @(slide.IsFirst() ? "class='active'" : null)

    The output is this:

    <li data-target="#portfolio-slideshow" data-slide-to="0" class=&#39;active&#39;></li>
    <li data-target="#portfolio-slideshow" data-slide-to="1"></li>
    <li data-target="#portfolio-slideshow" data-slide-to="2"></li>

    How can i get rid of the "&#39;" before and after active? It looks like it renders out the HTML number for the single quotes.

    If i remove the single quotes then the HTML output is like this missing the double quotes around "active":

    <li data-target="#portfolio-slideshow" data-slide-to="0" class=active></li>

    Thanks!

    // René

  • René Andersen 238 posts 684 karma points
    Mar 17, 2015 @ 09:18
    René Andersen
    0

    Hi Jan

    I found a solution and now i use this code:

    Html.Raw("class=\"active\"") : null)

    // René

Please Sign in or register to post replies

Write your reply to:

Draft