We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17851
    • 213 Posts
    I’ve got a list of articles I’m displaying with Ditto. What I want is to be able to click an article to view the detail, and on that detail page, have a "Prev" and "Next" item so I don’t have to go back to the list of articles. I’ve tried Ditto and the tplCurrentDocument template, but I’m not sure that’s what I need to use. Is there a better snippet for doing this?
      Mark Macatee
      President
      Power 10 Solutions
      http://www.power10solutions.com
      • 7231
      • 4,205 Posts
      I think Ditto can handle this as part of the pagination features. Also there is a snippet name PrevNext (and there is also PrevNextJump) that can do this.
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 17851
        • 213 Posts
        Yes, that’s what I was doing, using pagination, limit=1, and tplcurrentdocument, but never got it completely right. hmmm, that snippet looks promising...i’ll give it a shot.
          Mark Macatee
          President
          Power 10 Solutions
          http://www.power10solutions.com
          • 5811
          • 1,717 Posts
          I’ve got a list of articles I’m displaying with Ditto. What I want is to be able to click an article to view the detail, and on that detail page, have a "Prev" and "Next" item so I don’t have to go back to the list of articles.
          Coming back with the same question >:(

          I understand that pagination features is using to paginate the list of articles on several pages. Not to jump from an article to the following.

          And the snippet PrevNextJump generates links to navigate between documents in a directory. Which is not the same thing that to navigate between documents which belongs to a ditto result list.

          So does it exist a solution to do a true navigation between articles of a ditto list ?

          EDIT: I found this old post. Ditto prev next record feature is recorded as a potential feature of version 2.5 Any news about this update ?
            • 10449
            • 956 Posts
            An easily overlooked Ditto feature is the "save" parameter. Use Ditto to generate a multidimensional array, and then do your own logic with the result-set.

            e.g.

            [!Ditto? &id=`dittoTestTwo` &save=`3` &parents=`81` &depth=`5` &display=`all` &total=`100` &sortBy=`menuindex` &sortDir=`ASC`!]
            [!dittoResource? &dittoID=`dittoTestTwo`!]
            



            Snippet dittoResource:

            <?php
            
            if($dittoID) {
            	$resource = $modx->getPlaceholder($dittoID . "_ditto_resource");
            	echo "<pre>";
            	print_r($resource);
            	echo "</pre>";
            }
            
            
            /*
            
            each $resource array item shows something like:
            
            [id] => 83 
            [pagetitle] => foo 
            [introtext] => bar
            [createdby] => 1 
            [published] => 1 
            [createdon] => 1197126774 
            
            */
            
            ?>
            


            Obviously, the above snippet usage won’t create the "previous" / "next" navigation. But with a few lines of PHP, this should be fairly easy, I guess...
              • 10449
              • 956 Posts
              Here’s a basic example:

              <?php
              
              $dittoID = "dittoTestTwo";
              
              
              $p['id'] = $dittoID;
              $p['parents'] = 47;
              $p['save'] = 3;
              $p['depth'] = 5;
              $p['sortBy'] = 'menuindex';
              $p['sortDir'] = 'ASC';
              $p['total'] = 99;
              $p['display'] = 'all';
              $snippet = $modx->runSnippet('Ditto', $p);
              
              
              $resource = $modx->getPlaceholder($dittoID . "_ditto_resource");
              $currID = $modx->documentIdentifier;
              
              
              foreach($resource as $k=>$v) {
              	$idList[$k] = $resource[$k]["id"];
              }
              
              $docCount = count($idList);
              $key = array_search($currID, $idList);
              
              
              // create "previous" link?
              if($key > 0) {
              	$prevLink = $modx->makeUrl($resource[$key-1]["id"], '', '', 'full');
              	$prevPT = $resource[$key-1]["pagetitle"];
              	$output = "< previous page: <a href=\"$prevLink\">$prevPT</a> ";
              }
              
              
              
              
              // create "next" link?
              if( $key < $docCount-1 ) {
              	$nextLink = $modx->makeUrl($resource[$key+1]["id"], '', '', 'full');
              	$nextPT = $resource[$key+1]["pagetitle"];
              	$output .= "next page: <a href=\"$nextLink\">$nextPT</a> >";
              }
              
              echo $output;
              
              
              ?>
              


              Of course, if I’d use this in a real website, the Ditto vars would come from outside, to make it really flexible (parent id etc.)
                • 5811
                • 1,717 Posts
                Thks GaneshXL for this answer.

                But as i understand (and tested) this solution requires, inside the document, to recall the ditto call which set up the result id list. Unfortunately, in the document you don’t know how has been set up the list. You could arrive on a document by several different ways with various ditto calls.

                Perhaps the solution is to set up the result list of id "somewhere" and then re-use this list of id by the PrevNextDittoList snippet in the document.
                Somewhere could be a post parameter or a cookie. What do you think of this idea ?



                  • 10449
                  • 956 Posts
                  I’m not sure I understand. You can make this as flexible as you want. The "glue" is the Ditto id parameter, hence you could use this multiple times per page/template. You could define two prev/next navigation items: e.g. one for books (level 1), another for chapters (level 2). Just use two different id-parameters.

                  You could place the Ditto call in the document or template in the usual way: [[Ditto? &id=`foo1` &save=`3` ...]]
                  and then use [!dittoPrevNext? &dittoID=`foo1` ...!] somewhere else. It all depends how your site is organized and structured (templates/chunks).

                  Of course, you could save the id list in a session var. But even so, PHP still needs to define which doc is next, previous, and current on a page-by-page basis.
                    • 5811
                    • 1,717 Posts
                    Ok, i will try to explain where I think are the limits of this solution.

                    A first ditto call selects the 10 latest books of a shop: [!Ditto &id=`latest` &parents=`15` &paginate=`1` &display=`10` &sortBy=`absDatePub`!]
                    This first Ditto call provides a first list of book: book1, book2, book3, book4, ...

                    A second ditto call (an another menu item) selects the 5 best books selected by customers: [!Ditto &id=`best` &parents=`15` &paginate=`1` &display=`5` &sortBy=`absAvis`!]
                    This second call provides for instance : book2, book4, book8, book12, book21

                    Whatever the name of id (best ou latest) , you need to know the correct ditto call to get the right result id list when you use &save=`3` on the document.

                    If you put [!Ditto &id=`latest` &save=`3` &parents=`15` &paginate=`1` &display=`10` &sortBy=`absDatePub`!] on the book#4 and arrives on the document after using the "better" item menu, your Prev/next navigation will be wrong.

                    Same thing, if you put : [!Ditto &id=`best` &parents=`15` &paginate=`1` &display=`5` &sortBy=`absAvis`!] on the book#4 and arrives on the document after using the "latest" item menu.

                    When you execute the ditto call to get the document list, you set up the document list. But after choosing a document (read more ... link), when you arrive on the document, you have lost the context of the ditto call and you are on a different page. And you don’t know how you arrive on this page (best or latest ditto list)

                    So rather to keep and pass the context (to replay the ditto call with the save option, it could be better to save and pass the result id list when the ditto call is executed to set up the list of document.
                      • 10449
                      • 956 Posts
                      Well, you could simply use query-strings and/or session vars.
                      Add &dittoNavType=better or something like that.
                      Store the id lists in $_SESSION["navNewest"] / $_SESSION["navBetter"].

                      To avoid creating those session vars on every page load, use something like
                      if(empty($_SESSION["navNewest"])) {
                      	// run snippet, create the array, store in session-var...
                      }