We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10988
    • 14 Posts
    Hi

    I’ve been using the "If" snippet to render one of two Wayfinder calls based on the presence of a placeholder. Essentially, this
    [[!If?
        &subject=`[[+cat]]`
        &operator=`isnotempty`
        &then=`[[Wayfinder? &displayStart=`true` &hereId=`[[+cat]]` &startId=`[[UltimateParent? &id=`[[+cat]]` &top=`108`]]`]]`
        &else=`[[Wayfinder? &startId=`108` &level=`1`]]`
    ]]
    


    What I’ve just come to realise is that both "then" and "else" parameters are evaluated fully as the MODx parser works from the inside out. This is of course terribly inefficient.

    Based on a brief Twitter discussion with Jay Gilmore, I think there may be a better way. Any thoughts?

    Cheers,
    Phil
      • 27708 MODX Staff
      • 2,502 Posts
      You have a few things going on here not the least of which is a placeholder being the subject of the If call? What is the [[+cat]]? I also don’t know (as I haven’t tested it but what happens once those Wayfinder calls get cached? You could optionally put the If snippet inside wayfinder call and swap out the paramaters based on result.

      If you want to use a snippet you are going to have to evaluate the output/value of the [[+cat]] placeholder. You’ll want something like the following (Warning I am not a dev):
      <?php
      
      $cat = $modx->getPlaceholder('cat');
      if(!$cat || $cat == ''){
      $params = array('displayStart'=> 'true', 'hereId'=> $cat, 'startId'=> $modx->runSnippet('UltimateParent',array('id'=>$cat, 'top'=>'108'));
      }
      
      else{
      $params = array('startId'=>'108', 'level'=>'1');
      }
      $o = $modx->runSnippet('Wayfinder', $params);
      
      return $o;
        
      ?>
      

        Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
        • 10988
        • 14 Posts
        Thanks Jay, you’ve given me some food for thought.

        I might try something like the following syntax

        [[Wayfinder? &hereId=`[[+cat:default=`[[*id]]`]] ...
        


        I should be able to determine the correct parameter value to use in each case by using the [tt]default[/tt] output filter and some PHx syntax.

        FYI [tt][[+cat]][/tt] is assigned from a URL parameter (using a snippet I wrote called [tt]RequestToChunk[/tt])
          • 33968
          • 863 Posts
          Quote from: Phil at May 12, 2011, 08:58 AM

          FYI [tt][[+cat]][/tt] is assigned from a URL parameter (using a snippet I wrote called [tt]RequestToChunk[/tt])
          In that case, couldn’t you extend that snippet to do your If..then..else logic too?
            • 27708 MODX Staff
            • 2,502 Posts
            You could possibly use output filters inside the Wayfinder call but they are not necessarily going to be faster than one snippet. You’d have to try it to see.
              Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
              • 10988
              • 14 Posts
              Quote from: lucas at May 12, 2011, 09:33 AM

              In that case, couldn’t you extend that snippet to do your If..then..else logic too?

              It’s a generic snippet to pass request parameters to any chunk as placeholders that I use in various places.

              I may consider creating a more specialised version for the above use case though.
                • 33968
                • 863 Posts
                How about this...

                In your template:
                [[!ifCat? &cat=`[[+cat]]`]]
                

                And the snippet:
                <?php
                $output = '';
                
                if (!empty($cat)) {
                    $output = '[[Wayfinder? &displayStart=`true` &hereId=`' . $cat . '` &startId=`[[UltimateParent? &id=`' . $cat . '` &top=`108`]]`]]';
                }
                else {
                    $output = '[[Wayfinder? &startId=`108` &level=`1`]]';
                }
                
                return $output;
                


                You could also use $modx->runSnippet to run Wayfinder and UltimateParent from inside this snippet. Would be interested to know which is quickest...
                  • 27708 MODX Staff
                  • 2,502 Posts
                  From what I recall sending the Wayfinder calls out as output won’t get parsed. I could be wrong there but my method yields the same result and uses the API directly vs. the parser. It should be faster.
                    Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub