Jump to:

3435 Posts in 1026 Topics by 866 members

Template Questions

SilverStripe Forums » Template Questions » $FirstLast: what when the first is also the last?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 3897 Views
  • yurigoul
    Avatar
    Community Member
    202 Posts

    $FirstLast: what when the first is also the last? Link to this post

    People here probably know that within a control statement you can for example use class="$FirstLast" to add class="first" to the first item and class="last" to the last.

    This might be nitpicking but what if the first is also the last? Right now it adds class="first" but shouldn't it add class="first last" or better class="firstlast". In that way you have more control over your layout with css.

    I could probably create a function for this, but I just wanted to ask the people here their opinion.

  • baba-papa
    Avatar
    Community Member
    279 Posts

    Re: $FirstLast: what when the first is also the last? Link to this post

    There is a method called FirstLast in the ViewableData.php:

       /**
        * Returns 'first' if this item is the first in the container set.
        * Returns 'last' if this item is the last in the container set.
        */
       function FirstLast() {
          if($this->iteratorPos == 0) {
             return "first";
          } else if($this->iteratorPos == $this->iteratorTotalItems - 1) {
             return "last";
          } else {
             return "";
          }
       }

    You could overwrite it in your Page class with further functionality

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: $FirstLast: what when the first is also the last? Link to this post

    I know and I could. The only reason why I posted it here was to find out if there were other people who think this should be standard behavior.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: $FirstLast: what when the first is also the last? Link to this post

    You can use $First and $Last to achieve exactly what you want for styling purposes, so it's not really an issue of not having control over styling. If anything, "first last" might be more technically accurate (especially for javascript behaviours), but from a styling point of few it probably has as many issues as just "first".

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: $FirstLast: what when the first is also the last? Link to this post

    div {padding: 30px 0;}
    div.first {padding-top: 0;}
    div.last {padding-bottom: 0;}
    div.first.last {padding-bottom: 0; padding-top: 0;}

    The last one might not even be necessary in this example, but the FirstLast function will probably return something like .firstlast and not .first.last

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: $FirstLast: what when the first is also the last? Link to this post

    Well, no, I would definitely not expect it to return "firstlast". I might expect it to return "first last", because the item is both a first item and a last item, but I would not want it to return a new, distinct, firstlast type (in which case, it would be better to call it a 'singleitem' or something meaningful). Makes no sense to require a whole new selector style that only applies to single items.

    You are right, in your example div.first.last would not be required, but could be used to override styles from the previous selectors.

  • jm78
    Avatar
    Community Member
    1 Post

    Re: $FirstLast: what when the first is also the last? Link to this post

    I recently asked myself this question. I noticed that $FirstLast would only return "first" when an item was both first and last.

    I solved this by: class="$FirstLast <% if Last %>last<% end_if %>"

    If the item in your control loop is both first and last this will return: class="first last" (note the space)

    In your CSS both .first {} *and* .last {} classes will be applied.

  • MarijnKampf
    Avatar
    Community Member
    164 Posts

    Re: $FirstLast: what when the first is also the last? Link to this post

    Ran into the same problem, I would add my vote for changing the default behaviour to return 'first last', as this is more accurate and logical in my opinion.

    3897 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.