Created
December 4, 2011 22:34
-
-
Save markjames/1431509 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
SiteTree::add_extension('ViewableData','ExtsViewableDataDecorator'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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