We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21560
    • 145 Posts
    This is an auto-generated topic for BasicNav 1.1-rc2 by nimja.

    Brief Description:

    Version 1.1
    First/Last now dissapear properly.
    Links encased in Divs for layout purposes.
    Output to placeholder (optional) [[!+basicnav]]
    Added default properties, because it's cool.
      [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
      • 19309
      • 49 Posts
      It seems that BasicNav would be the solution I’m searching for: displaying simple next/previous links (within the current folder).

      But it seems that I’m not smart enough to get this snippet going sad
      I don’t get any output, when I use it in a template for pages within a folder.
      The only time I see anything in the frontend is when I use the snippet in a template for a top level page. Then I receive the error message “No parent?”, which is correct – so the snippet is working.

      Are there any hints or other code examples apart from what is included in the snippet comments?
        • 10257
        • 55 Posts
        Any code? How did you call the snippet?

        I use it successfully at one site....
          • 19309
          • 49 Posts
          Quote from: fthieme at Jan 11, 2011, 11:27 AM

          Any code? How did you call the snippet?

          Sorry, I didn’t include the code.

          [[!BasicNav &sortBy=`menuindex` &sortDir=`ASC`]]
          


          This line is part of a chunk which is part of a template. (I also included it in the template directly with the same result.) The frontend output is just a blank line. All resources in the folder as well as the folder resource itself are published and not marked as excluded for menus.

          Or do I have to use the placeholder in this case? Usually I don’t have problems with snippet calls, but here I’m a little confused.
            • 10257
            • 55 Posts
            maybe it’s just the ’?’ after BasicNav missing?
              • 19309
              • 49 Posts
              OMG, how could I overlook that? undecided That’s really awkward.

              Thanks for the help!
                • 21560
                • 145 Posts
                I’m really happy people are using this. All working well?
                  [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
                  • 30703
                  • 51 Posts
                  I had the same problem as the previous person lol - forgot the ’?’ on the snippet call - yes it’s a silly mistake, but that’s exactly how it’s written in the samples so maybe that should be corrected wink

                    Tom Dyer

                    Designer/Animator/Web Developer - England, UK.
                    http://www.bespokepixels.co.uk
                    • 19309
                    • 49 Posts
                    Quote from: Nimja at Jan 18, 2011, 08:59 AM

                    I’m really happy people are using this. All working well?

                    Mostly, but there is a number of bugs. That’s why I sat down and fixed and enhanced the snippet a little bit – as far as this was possible with my limited PHP knowledge.

                    These are my changes and enhancements:


                    • The original snipped had an inconsistence by documenting the sorting parameter “sortDir” but internally using the variable “sortHow”. So the parameter “&sortDir” in the snippet call had no effect. I changed the internal variable name to “sortDir”, so now everything works like it is documented. (“sortDir” also matches the standard used in other snippets.)
                    • The “placeholder” parameter did’t work in the original script, because setting it to “0” was the same as omitting it at all. The default value always kicked in. I changed the default to “0”. So now you really have the choice if you want to use the placeholder or if you want the direct output.
                    • The default arrows in “prevText” and “nextText” were interchanged. I fixed that.
                    • I added parameters to hide the output of any of the four links. E.g. useful if only next and previous links or only first and last links should be displayed.
                    • I added a parameter “currentPosition”. It can hold the ID of the resource which marks the current position in the page tree level you want to get the next and previous resource from. Sounds complicated, but it is useful, if you want to “break out” of the current folder. See the detailed documentation and example in the comment area of the script below.

                    And here is my new version of the script. Acquire it for the repository or refine it, just as you like. Caution: It is not downward compatible, since I had to change default values.

                    <?php
                    /* BasicNav by Nimja.com
                       (enhanced a little bit by michael-van-laar.de)
                    
                    Snippet call (example with all optional parameters):
                    [[!BasicNav? &sortBy=`publishedon` &sortDir=`ASC` &currentPosition=`[[*id]]` &placeholder=`0` &hideFirst=`0` 
                    &hidePrev=`0` &hideNext=`0` &hideLast=`0` &firstTxt=`<<` &prevTxt=`<` &nextTxt=`>` &lastTxt=`>>`]]
                    
                    Placeholder (if &placeholder=`1` is used in the snippet call):
                    [[!+basicnav]]
                    
                    Snippet call and placeholder MUST be called uncached, otherwise pages in the middle would not know when a new last page is added.
                    
                    
                    Parameters (all optional) in detail :
                    
                    &sortBy            Field containing the sorting order criterion          Default: publishedon
                    
                    &sortDir           Sorting order (ASC or DESC)                           Default: ASC
                    
                    &currentPosition   ID of the resource which marks the current position   Default: (ID of current resource)
                                       in the page tree level you want to get the next and
                                       previous resource from.
                                       YOU ONLY NEED THIS PARAMETER IF YOU WANT TO "BREAK
                                       OUT" OF THE CURRENT FOLDER.
                                       
                                       Example:
                                 
                                       In the following resource structure (page tree) you want to include links
                                       to the next and previous project on every "project detail" page.
                                       In this case you have to set &currentPosition to [[*parent]]. By doing this
                                       the next and previous links on every "project detail" page don't refer to
                                       the next and previous "project detail" page (as they normally would), but to
                                       the next and previous "project" page.
                               
                                       + Project overview
                                       |
                                       +--+ Project 1
                                       |  +--- Project detail page 1.1
                                       |  +--- Project detail page 1.2
                                       |
                                       +--+ Project 2
                                       |  +--- Project detail page 2.1
                                       |  +--- Project detail page 2.2
                                       |
                                       +--+ Project 3
                                          +--- Project detail page 3.1
                                          +--- Project detail page 3.2
                    
                                       In concrete terms:
                                       Placing the snippet and placeholder call
                                           [[!BasicNav? &currentPosition=`[[*parent]]`]] [[!+basicnav]]
                                       on page "Project detail page 2.1" would generate one link to page
                                       "Project 1" and one link to page "Project 3".
                    
                    &placeholder       Output is stored in placeholder (see above)           Default: 0 (= FALSE)
                    
                    &hideFirst         "First link" will never be displayed. (Useful if      Default: 0 (= FALSE)
                                       only next and previous links should be displayed.)
                    
                    &hidePrev          "Previous link" will never be displayed. (Useful if   Default: 0 (= FALSE)
                                       only first and last links should be displayed.)
                    
                    &hideNext          "Next link" will never be displayed. (Useful if       Default: 0 (= FALSE)
                                       only first and last links should be displayed.)
                    
                    &hideLast         "Last link" will never be displayed. (Useful if        Default: 0 (= FALSE)
                                       only next and previous links should be displayed.)
                    
                    &firstTxt         Link text of "first link"                              Default: <<
                    
                    &prevTxt          Link text of "previous link"                           Default: <
                    
                    &nextTxt          Link text of "next link"                               Default: >
                    
                    &lastTxt          Link text of "last link"                               Default: >>
                    
                    */
                    
                    
                    // ---- Default Values forced.
                    
                    $sortBy = !empty($sortBy) ? $sortBy : 'publishedon';
                    $sortDir = !empty($sortDir) ? $sortDir : 'ASC';
                    $placeholder = !empty($placeholder) ? $placeholder : FALSE;
                    $cur = !empty($currentPosition) ? $currentPosition : $modx->resource->id;
                    
                    $hideFirst = !empty($hideFirst) ? $hideFirst : FALSE;
                    $hidePrev = !empty($hidePrev) ? $hidePrev : FALSE;
                    $hideNext = !empty($hideNext) ? $hideNext : FALSE;
                    $hideLast = !empty($hideLast) ? $hideLast : FALSE;
                    
                    $firstTxt = !empty($firstTxt) ? $firstTxt : '<<';
                    $prevTxt = !empty($prevTxt) ? $prevTxt : '<';
                    $nextTxt = !empty($nextTxt) ? $nextTxt : '>';
                    $lastTxt = !empty($lastTxt) ? $lastTxt : '>>';
                    
                    // ---- Actual code 
                    
                    //Get the parents (and verify)
                    $curResource = $modx->getObject('modResource',$cur);
                    $parent = $curResource->get('parent');
                    if (empty($parent)) {
                      return 'No parent?';
                      exit;
                    }
                    
                    //Get the children.
                    $criteria = $modx->newQuery('modResource');
                    $criteria->where(array(
                       'parent' => $parent,
                       'hidemenu' => 0,
                    ));
                    $criteria->sortby($sortBy,$sortDir);
                    $children = $modx->getCollection('modResource',$criteria);
                    
                    $first = NULL;
                    $prev = NULL;
                    $next = NULL;
                    $last = NULL;
                    
                    $seen = FALSE;
                    $now = FALSE;
                    foreach ($children as &$child) {
                      //Check if this is the current child.
                      $now = ($child->id == $cur);
                      if ($now) $seen = TRUE;
                      
                      //First one is only set once.
                      if (!$hideFirst && !$seen && empty($first)) $first = $child;
                      //Prev is set until we meet our current.
                      if (!$hidePrev && !$seen) $prev = $child;
                      //Next is set once directly after the current.
                      if (!$hideNext && $seen && empty($next) && !$now) $next = $child; 
                      //Set after we've seen stuff.
                      if (!$hideLast && $seen && !$now) $last = $child;
                    }
                    //Disable certain things at times.
                    if (!$hideFirst && ($first == $prev)) $first = NULL;
                    if (!$hideLast && ($last == $next)) $last = NULL;
                    
                    $result = '<div class="nav">';
                    if (!$hideFirst) $result .= empty($first) ? '<div></div>' : '<div><a href="[[~'.$first->id.']]" class="first" title="'.$first->pagetitle.'">'.$firstTxt.'</a></div>';
                    if (!$hidePrev) $result .= empty($prev) ? '<div></div>' : '<div><a href="[[~'.$prev->id.']]" class="prev" title="'.$prev->pagetitle.'">'.$prevTxt.'</a></div>';
                    if (!$hideNext) $result .= empty($next) ? '<div></div>' : '<div><a href="[[~'.$next->id.']]" class="next" title="'.$next->pagetitle.'">'.$nextTxt.'</a></div>';
                    if (!$hideLast) $result .= empty($last) ? '<div></div>' : '<div><a href="[[~'.$last->id.']]" class="last" title="'.$last->pagetitle.'">'.$lastTxt.'</a></div>';
                    $result .= '</div>';
                    
                    if ($placeholder) {
                      $modx->setPlaceholder('basicnav', $result);
                      return;
                    } else {
                      return $result;
                    }

                      • 30703
                      • 51 Posts
                      I noticed those errors in the snippet too - but I didn’t correct them all, just worked around them to get this current project done.
                      I think I’ll use this updated version next time though smiley thanks!

                        Tom Dyer

                        Designer/Animator/Web Developer - England, UK.
                        http://www.bespokepixels.co.uk