Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions /

$FirstLast: what when the first is also the last?


Reply


8 Posts   4167 Views

Avatar
yurigoul

Community Member, 202 Posts

13 February 2010 at 4:55am

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.

Avatar
baba-papa

Community Member, 279 Posts

15 February 2010 at 12:40pm

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

Avatar
yurigoul

Community Member, 202 Posts

16 February 2010 at 1:14am

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. :-)

Avatar
Hamish

Community Member, 712 Posts

16 February 2010 at 5:58am

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".

Avatar
yurigoul

Community Member, 202 Posts

16 February 2010 at 7:12am

Edited: 16/02/2010 7:14am

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 :-)

Avatar
Hamish

Community Member, 712 Posts

16 February 2010 at 8:01am

Edited: 16/02/2010 8:01am

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.

Avatar
jm78

Community Member, 1 Post

24 November 2010 at 9:26am

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.

Avatar
MarijnKampf

Community Member, 164 Posts

15 February 2011 at 12:40am

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.