Today I needed ditto to give the first and the last item in a list some special treatment.
I wanted to have something like this:
1|2|3|4
The usual css tricks to accomplish this unfortunately don’t work in any environment. Yet, if you have a class="last" on your last item, it becomes quite easy to do.
As far as I know, ditto can’t do this out of the box, so I have added this feature. I don’t want to mess around with the official code (which btw is extremely well commented...), so I’ll post here the necessary steps for anybody interested to do this.
We already have templates for &tplPaginateCurrentPage (the current page) and &tplPaginatePage (all other pages).
To give the first and the last item individual template, we’ll need 4 additional parameters:
&tplPaginatePageFirst
&tplPaginatePageLast
&tplPaginateCurrentPageFirst
&tplPaginateCurrentPageLast
Here we go:
Start with a backup of your ditto snippet and a backup of assets/snippets/ditto/classesditto.class.inc.php
Now, open the ditto snippet and replace
$ditto->paginate($start, $stop, $total, $display, $tplPaginateNext, $tplPaginatePrevious, $tplPaginateNextOff, $tplPaginatePreviousOff, $tplPaginatePage, $tplPaginateCurrentPage, $paginateAlwaysShowLinks, $paginateSplitterCharacter);
// generate the pagination placeholders
with
// pepebe: ditto hack to add individual templates for first and last item in each list
//pepebe: Add default templates if none are given by the ditto call
$tplPaginatePageFirst = isset($tplPaginatePageFirst)? $ditto->template->fetch($tplPaginatePageFirst) : "<span class='first'>[+page+]</span>";
$tplPaginatePageLast = isset($tplPaginatePageLast)? $ditto->template->fetch($tplPaginatePageLast) : "<span class='last'>[+page+]</span>";
$tplPaginateCurrentPageFirst = isset($tplPaginateCurrentPageFirst)? $ditto->template->fetch($tplPaginateCurrentPageFirst) : "<span class='first current'>[+page+]</span>";
$tplPaginateCurrentPageLast = isset($tplPaginateCurrentPageLast)? $ditto->template->fetch($tplPaginateCurrentPageLast) : "<span class='last current'>[+page+]</span>";
$ditto->paginate($start, $stop, $total, $display, $tplPaginateNext, $tplPaginatePrevious, $tplPaginateNextOff, $tplPaginatePreviousOff, $tplPaginatePage, $tplPaginateCurrentPage, $paginateAlwaysShowLinks, $paginateSplitterCharacter, $tplPaginatePageFirst, $tplPaginatePageLast, $tplPaginateCurrentPageFirst, $tplPaginateCurrentPageLast);
// generate the pagination placeholders
// End ditto hack
Save snippet and open classesditto.class.inc.php
On line 1120 you will find
for ($x = 0; $x <= $totalpages -1; $x++) {
$inc = $x * $summarize;
$display = $x +1;
if ($inc != $start) {
...
}
else {
...
}
}
replace the whole block with
// pepebe: ditto hack to add individual templates for first and last item in each list
for ($x = 0; $x <= $totalpages -1; $x++) {
$inc = $x * $summarize;
$display = $x +1;
if ($inc != $start) { // not current page
if($x == 0){ //first, but not current page
$pages .= $this->template->replace(array('url'=>$this->buildURL("start=$inc"),'page'=>$display),$tplPaginatePageFirst);
}
elseif($x == $totalpages-1){ //last, but not current page
$pages .= $this->template->replace(array('url'=>$this->buildURL("start=$inc"),'page'=>$display),$tplPaginatePageLast);
}
else { //not current
$pages .= $this->template->replace(array('url'=>$this->buildURL("start=$inc"),'page'=>$display),$tplPaginatePage);
}
}
else { // current page
if($x == 0){ //first, but not current page
$pages .= $this->template->replace(array('page'=>$display),$tplPaginateCurrentPageFirst);
}
elseif($x == $totalpages-1){ //last, but not current page
$pages .= $this->template->replace(array('page'=>$display),$tplPaginateCurrentPageLast);
}
else{ //not current
$pages .= $this->template->replace(array('page'=>$display),$tplPaginateCurrentPage);
}
$modx->setPlaceholder($dittoID."currentPage", $display);
}
}
// End: ditto hack
Save and add the new parameters to your ditto call (or use the default values). That’S it. Now you can give individual styles to the last and the fist item in the list.
pepebe