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

    I have a question about ’return’ usage inside snippets.

    function functionOne() {
    return ’A String ’;
    }
    functionOne(); //Shouldn’t output anything to the page
    echo functionOne(); //Should output ’A String ’
    return ’String Two’; //Shouldn’t output anything to the page

    The result of the above code is:
    A String String Two

    The return that is not inside the function is acting like an echo()...

    In a standard php script the result from the same code is:
    A String


      • 7923
      • 4,213 Posts
      Output from snippet in MODx should be always returned at the end of the snippet..

      eg:

      <?php
      $output = '';
      
      function boldText($text) {
          return '<b>'.$text.'</b>';
      }
      
      $output .= "Here's some bolded text: ";
      $output .= boldText("I'm bolded");
      
      return $output;
      ?>


      echo also works in snippets, but the output won’t necessarily come to the place where you expect it to be on the page. So get your snippet output in a variable and return it at the end, don’t use echo. See also this.

      This is only valid for Evolution though, AFAIK in Revolution you can use echo just fine in a snippet.

      What comes to your code above, these lines print that output from your snippet:

      echo functionOne(); //Should output ’A String ’
      return ’String Two’; //Shouldn’t output anything to the page


        "He can have a lollipop any time he wants to. That's what it means to be a programmer."
        • 3749
        • 24,544 Posts
        Your code acts just as expected in MODx.

        MODx outputs whatever is returned from the snippet, as well as anything that’s echo’d in the snippet.

        functionOne(); //Shouldn't output anything to the page
        echo functionOne(); //Should output 'A String '
        return 'String Two'; //Shouldn't output anything to the page


        The first line doesn’t output anything.
        The second line echos the return value of the function so it puts out "A String"
        The third line, with a return outside a function, returns "String Two" from the snippet which MODx then outputs to the screen.

        Hope this helps you understand how MODx works.

          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
          • 18839
          • 39 Posts
          Thanks for the replies guys.

          So when using ’return’ inside snippets, is this true?

          1.) A ’return’ inside a function, and that function is called, nothing is echo’d/printed to the page
          2.) A ’return’ outside a function works exactly the same as echo or print, since MODx echo’s whatever is returned from a snippet

          function functionName() {
              return 'String Value';
          }
          functionName(); //Does not echo anything to the page
          echo functionName(); //Does exactly the same as the line below
          return functionName(); //Echos 'String Value' to the page


          Thanks,

          Mark

            • 3749
            • 24,544 Posts
            You probably know this, but number 2 is only true if the return is executed.

            return (’hello’);

            return(’goodbye’);


            Will result in only ’hello’ being output.

            Return in a snippet ends the snippet immediately (as does exit() or die() ) and nothing more is executed within the snippet.

            Look closely at Doze’s example above it provides an excellent way of doing things in a snippet. If you use $output and then contcatenate your desired output to it with .= and then just have one return($output) statement in your snippet (never using echo or print), it will make your life *way* easier in the long run.

            If your snippet is at all complicated, you’ll also want to look into using $modx->setPlaceholder() and putting placeholders in a template chunk.

            That way you can set your snippet’s output to various placeholders anywhere in the snippet with no concerns about order or formatting. Then you just "return" the contents of the template chunk.

            return($modx->getChunk('chunkName'))

            and MODx does the rest for you by replacing the placeholders in the template with the values you’ve set before the page is sent to the browser.
              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
              • 18839
              • 39 Posts
              In a MODx snippet whatever is returned by the snippet is echoed, but in a standard PHP script return echoes nothing, it just returns a value

              The following lines of code at the end of a snippet do the exact same thing, i.e. print foo onto the page (the only exception being the return stops the parser executing the snippet)

              $foo = $modx->getPlaceholder(’bar’);
              echo $foo; //This line outputs to the screen
              print $foo; //This line outputs to the screen
              return $foo; //This line outputs to the screen and stops script execution. In a standard PHP script: return never outputs to the screen on its own

              Why does MODx automatically echo everything ’returned’ from a snippet?
                • 30223
                • 1,010 Posts
                Why does MODx automatically echo everything ’returned’ from a snippet?

                Because the standard function of a snippet is to insert something into your html document. Because snippets are such a powerful feature of modx people keep finding ways of utilizing them beyond that basic function. As long as you’re aware of it it’s easy to overcome this.
                  • 3749
                  • 24,544 Posts
                  And you always have the option of returning nothing if you have a snippet that you don’t want to produce output in the browser.

                  return("");
                    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
                    • 7231
                    • 4,205 Posts
                    I think that it is great that modx echos everything it returns. I find this to be very easy to work with. I just think of snippets being functions and will always use the return to generate the output, simple. The cool thing is that you can use either echo or return (although I recommend return).

                    Just curious, I have never run into a situation where this would be a problem, how is this causing a problem in your script?
                      [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&#39;t know what it is.
                      Do you, Mr. Jones? - [bob dylan]
                      • 18839
                      • 39 Posts
                      Hi guys,

                      Thanks for the replies.

                      "Just curious, I have never run into a situation where this would be a problem, how is this causing a problem in your script?"
                      I never ran into any problems with this, I’m just trying to get a better understanding of how returns are handled by the MODx parser.

                      Basically, all of my posts were asking the same questions:

                      (a)Why does a return outside a function in a MODx snippet automaticallly echo the output to the screen?
                      (b)If the MODx parser automatically echos returned output from a snipppet, why does a call to the function that contains a return not echo the results as well?
                      (c)Why is there a disparity between the way a ’return’ is handled by a standard PHP script and a MODx snippet?
                      <?
                      function echoSomething($string = 'foo') {
                      	return $string;
                      }
                      echoSomething(); //This will output nothing to the screen (same result in standalone PHP script and MODx)
                      echo echoSomething(); //This will output 'foo' to the screen (same result in standalone PHP script and MODx)
                      return echoSomething(); //This will output nothing to the screen in a standalone PHP script, but in a MODx snippet it will output 'foo' to the screen
                      ?>


                      The cool thing is that you can use either echo or return (although I recommend return).

                      I know a return looks a bit fancier than the oul faithfull echo, but with the exception of script execution (return false;), a return outside a function in a MODx snippet does the exact same thing as an echo... (it echoes the output to the screen)