We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 23018
    • 353 Posts
    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
      Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
      • 23018
      • 353 Posts
      Quick update:

      I have patched ditto some more to get rid of solitary link results (this happens when the total number of articles is less or equal then the &summarize parameter).



      		if($total>$summarize){
      			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);
      				}
      			}
      		}
      
      


      Note: If you want to speed up ditto in case $total<=$summarize, you may move the if statement towards the top just below function paginate(......). Leave the closing bracket just where it is on top of the $modx->setPlaceholder... codeblock.

      Have fun,

      pepebe
        Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
        • 26931
        • 2,314 Posts
        hey pepebe,

        thanks for sharing, maybe i get you wrong, but isn’t &tplFirst (User defined chunk to format the first document) / &tplLast (User defined chunk to format the last document) what you are looking for?
          • 23018
          • 353 Posts
          Well according to http://ditto.modxcms.com/files/snippet-ditto-php.html

          you have Templates for the first and the last Document. But you don’t have templates for the the first or last PAGINATION LINKS.

          You may use individual styles for the following items:

          tplPaginatePrevious Template for the previous link
          tplPaginateNext Template for the next link
          tplPaginateNextOff Template for the inside of the next link
          tplPaginatePreviousOff Template for the previous link when it is off
          tplPaginatePage Template for the page link
          tplPaginateCurrentPage Template for the current page link

          My hack adds the following templates

          &tplPaginatePageFirst The FIRST Link
          &tplPaginatePageLast The LAST Link
          &tplPaginateCurrentPageFirst The FIRST Links, if it is also the CURRENT LINK
          &tplPaginateCurrentPageLast The LAST link, if it is also the CURRENT LINK

          Regards,

          pepebe
            Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
            • 26931
            • 2,314 Posts
            aaaah - okay, thanks! *bookmarked*