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?


8 Posts   4011 Views


13 February 2010 at 4:55am Community Member, 202 Posts

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.


15 February 2010 at 12:40pm Community Member, 279 Posts

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


16 February 2010 at 1:14am Community Member, 202 Posts

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


16 February 2010 at 5:58am Community Member, 712 Posts

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


16 February 2010 at 7:12am (Last edited: 16 February 2010 7:14am), Community Member, 202 Posts

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


16 February 2010 at 8:01am (Last edited: 16 February 2010 8:01am), Community Member, 712 Posts

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.


24 November 2010 at 9:26am Community Member, 1 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.


15 February 2011 at 12:40am Community Member, 164 Posts

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.