We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 45767
    • 13 Posts
    My code call is to check where a page lays and if it has children as a smaller side bar navigation specific to that page.
    It works fine if i prefix some debugging code before it for example
    Parent: [[*parent]]

    [[!If?
       &subject=`[[!GetLevel]]`
       &operator=`EQ`
       &operand=`1`
       &then=``
       &else=`[[!If?
          &subject=`[[!GetLevel]]`
          &operator=`EQ`
          &operand=`2`
          &then=`[[!Wayfinder? &startId=`[[!If?
                                            &subject=`[[!HasChildren]]`                                   
                                            &operator=`EQ`
                                            &operand=`0`
                                            &then=`[[*parent]]`
                                            &else=`[[*id]]`]]
                  &rowTpl=`sideNav` &outerTpl=`outerTpl`]]`
          &else=`[[!Wayfinder? &startId=`[[*parent]]` &rowTpl=`sideNav` &outerTpl=`outerTpl`]]`
        ]]`
    ]]


    but not having the debug code will display this where the code would output:
    `[[!If? &subject=`2` &operator=`EQ` &operand=`2` &then=`[[!Wayfinder
      • 3749
      • 24,544 Posts
      Wow, that's do deeply nested that I can hardly parse it. I imagine the parser has a similar problem. I think the PHP code version would be shorter, easier to follow, easier to maintain, and quite a bit faster. I don't think either snippet tag has to be called uncached if you won't be adding children often, but try it this way first.

      Try this (untested):

      On the Page:

      [[!MySideBar]]


      Create a chunk called MyWfChunk:

      [[!Wayfinder? &startId=`[[+my_start]]` &rowTpl=`sideNav` &outerTpl=`outerTpl`]]


      Create a snippet called MySideBar:

      <?php
      /* MySideBar snippet */
      
      $level = (integer) $modx->runSnippet('GetLevel');
      
      if ($level == 2) {
         if ($modx->resource->hasChildren()) {
             $start = $modx->resource->get('parent');
         } else {
             $start = $modx->resource->get('id');
         }
         $output = $modx->getChunk('MyWfChunk', array('my_start' => $start));
      } else {
          $output = '';
      }
      return $output;

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 45767
        • 13 Posts
        Thanks Bob,
        Yeah, it started off rather simple but as I got further down the tree it started needing more conditionals. Never thought to take it back to the snippet, doh.

        With a bit of tweaking this worked perfectly for what I wanted, final code:

        <?php
        /* MySideBar snippet */
         
        $level = (integer) $modx->runSnippet('GetLevel');
         
        if ($level >= 2) {
           if ($modx->resource->hasChildren()) {
                $start = $modx->resource->get('id');
           } else {
               $start = $modx->resource->get('parent');
           }
           $output = $modx->getChunk('MyWfChunk', array('my_start' => $start));
        } else {
            $output = '';
        }
        return $output;
          • 3749
          • 24,544 Posts
          I'm glad it worked for you. BTW, the 'startId' value ('id' versus 'parent') is reversed from what you have in the tag above, are you sure that's what you want?

          Also, since the chunk is so small, you could put it in the snippet as well. do away with the chunk, and speed things up a little more:

          $output = "[[!Wayfinder? &startId=`" . $start . 
              "` &rowTpl=`sideNav` &outerTpl=`outerTpl`]]";
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting