We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7869
    • 118 Posts
    I created a test snippet called runSnippet.

    <?php
        echo $modx->documentObject['content'];
        $modx->runSnippet('AjaxSearch'); 
    ?>


    When I call this snippet from a template with [[runSnippet]] (or [!runSnippet!]), it will display the value of documentObject[’content’], but it will not run the AjaxSearch snippet. I’ve tested AjaxSearch on its own and it worked so I know it’s installed properly. I don’t know why it won’t work when called from a snippet. What am I missing?
      -Dorian
      • 7869
      • 118 Posts
      Hmm, I put an echo in front and now it works. I didn’t think that was necessary. huh If there’s a better solution, please post it.

      <?php
          echo $modx->documentObject['content'];
          echo $modx->runSnippet('AjaxSearch'); 
      ?>
        -Dorian
        • 10487 MODX Staff
        • 1,535 Posts
        Hmm, I put an echo in front and now it works. I didn’t think that was necessary.
        You need to use ’echo’ or capture the output into a variable and use ’return’ to output it to the page. The runSnippet() method always returns the string output of the snippet being called so it’s up to you to process the output.
          Garry Nutting
          Senior Developer
          MODX, LLC

          Email: [email protected]
          Twitter: @garryn
          Web: modx.com
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: DorianJ at Feb 24, 2009, 08:29 AM

          Hmm, I put an echo in front and now it works. I didn’t think that was necessary. huh If there’s a better solution, please post it.
          Try something like this:
          <?php
              $output = '<pre>';
              $output .= $modx->documentObject['content'] . "\n";
              $output .= $modx->runSnippet('AjaxSearch');
              $output .= '</pre>';
              return $output;
          ?>

          I prefer this to echo, explicitly returning the content from the snippet. This way the parser is in full control of where the output goes. Otherwise, it’s just echo’d into the output buffer at the point of execution.
            • 7869
            • 118 Posts
            Excellent, thanks! I’m curious though, was there any specific reason for the <pre> tags?
              -Dorian
              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: DorianJ at Feb 24, 2009, 11:37 AM

              Excellent, thanks! I’m curious though, was there any specific reason for the <pre> tags?
              No, was just exemplifying how to store the content into a string.