Skip to content

Instantly share code, notes, and snippets.

@markjames
Created December 4, 2011 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markjames/1431509 to your computer and use it in GitHub Desktop.
Save markjames/1431509 to your computer and use it in GitHub Desktop.
<?php
SiteTree::add_extension('ViewableData','ExtsViewableDataDecorator');
<?php
class ExtsViewableDataDecorator extends Extension {
/**
* Adds first/last/start/end class to a list item.
*
* Sometimes you need to:
* Lay out a load of thumbnails in a grid.
* Style only the first or last elements in a vertical/horizontal list of items
*
* You can do this using nth-child pseudoselectors, but it is dangerous.
*
* Instead, on any list of elements:
*
* - add a _first_ class to the very first one
* - add a _last_ class to the very last one
* - add a _start_ class to the first item on a row (e.g. every 4th item, starting with the first one)
* - add an _end_ class to the last item on a row (e.g. every 4th item, starting with the fourth one)
*
* @param $rowLength length of each row
* @return string list class
*/
public function ListClass( $rowLength=2 ) {
$classes = array();
// first
if ($this->owner->iteratorPos == 0) {
$classes[] = 'first';
}
// last
if($this->owner->iteratorPos == $this->owner->iteratorTotalItems - 1) {
$classes[] = 'last';
}
// start
if ($this->owner->iteratorPos % $rowLength == 0) {
$classes[] = 'start';
}
// end
if ($this->owner->iteratorPos % $rowLength == $rowLength - 1) {
$classes[] = 'end';
}
$html = join(' ', $classes);
return $html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment