We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 27708 MODX Staff
    • 2,502 Posts
    I am sure someone will be able to explain this more accurately since I am not as developer but the snippets are run through the PHP eval().

    In addition you are not returning/echoing to a browser you are returning/echoing to another script--the parser that then outputs the value from your snippet to the browser. I don’t know if you typically echo to another script but directly to page.


    Again, I’m not a dev and this is just how I’ve come to understand it.
      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
      • 36702
      • 76 Posts
      Quote from: mb07 at Oct 12, 2008, 07:52 AM

      (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?

      (a) That’s the purpose of a snippet, as explained above
      (b) If YOU call a function, the result is returned to YOU, not to MODx
      (c) see (a)
        • 18839
        • 39 Posts
        Snippets are useful for adding logic to websites. They can be used to create menus, determine who is logged in, or any other thing possible with the API.

        I don’t think snippets are always used print out to the screen, so that’s not the purpose of a snippet, IMO data ’returned’ from them should not automatically be echoed...
        If you want screen output: echo, if you want to process returned data: return.

        If snippets automatically echo returned data, than that’s how snippets work
          • 27708 MODX Staff
          • 2,502 Posts
          Quote from: mb07 at Oct 12, 2008, 08:48 AM

          I don’t think snippets are always used print out to the screen...

          No but typically, thats what snippets do: Return value(s) upon completed operation.

          I don’t think you would return a value to another snippet (you can if you want) but typically you are just performing operations and returning output. For triggered events you are going to use a plugin. It will perform an operation based on an action and you can trigger snippets, or any number of actions.

          PHP that operates on events are what plugins are for so they wouldn’t necessarily return a value but might insert some data, send an email, add a document, create a folder.
            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
            • 3749
            • 24,544 Posts
            Quote from: mb07 at Oct 12, 2008, 08:48 AM


            I don’t think snippets are always used print out to the screen, so that’s not the purpose of a snippet, IMO data ’returned’ from them should not automatically be echoed...

            Sending the return value from a snippet to the browser as output, as we’ve explained, is how MODx is designed and has worked for a long, long time. It won’t change, in part because changing it would break hundreds of existing snippets and trash countless web sites. But it also works well, AFAIK, for everyone here. When we want a process that returns a value that shouldn’t go on the screen, we put it in a function. Since MODx (and PHP) are for web sites, the assumption that you want to put something on the screen eventually is no too far fetched.

            If you don’t agree that it’s a good idea to output snippet return values, you’re free to use another CMS that you like better, or to write your own. As we often say here, MODx isn’t for everyone. You can also use MODx, and create your own entity that behaves as you prefer. Or you can easily modify the MODx document parser so that it throws away the return value from a snippet rather then echoing it. Or you can, as we’ve pointed out, always use echo in your snippets and always return an empty string from them, which will make MODx behave as you think it should.

            Further discussion of what MODx "should" do with snippet return values is kind of pointless, though, and borders on trolling.

              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
              • 22303 MODX Staff
              • 10,725 Posts
              Quote from: mb07 at Oct 12, 2008, 08:48 AM

              I don’t think snippets are always used print out to the screen, so that’s not the purpose of a snippet, IMO data ’returned’ from them should not automatically be echoed...
              If you want screen output: echo, if you want to process returned data: return.

              If snippets automatically echo returned data, than that’s how snippets work
              MODx encourages developers to use server-side logic as it was intended, to perform operations, not present data to users. A snippet can operate on data and return content to the templating engine. Returning anything else would be meaningless in string format, and this also reinforces the idea that logic should set data for the view, but let the templating engine allow designers to organize that presentation outside of the PHP code. By echo’ing content, you are automatically forcing a designer into modifying code to change the output.
                • 18839
                • 39 Posts
                Thanks for the reply OpenGeek, all my posts are nearly identical, I was looking for an dev answer as to why snippets were automatically echoing returned output, got the answer, thanks smiley

                If you don’t agree that it’s a good idea to output snippet return values, you’re free to use another CMS that you like better, or to write your own. As we often say here, MODx isn’t for everyone.

                I’ve used a lot of different CMS systems and by far MODx is light years ahead of the rest, maybe I’ll try the bloatware that is Drupal, or maybe give J! a whirl, none of them compare in scalabilty, functionality or ease of use that MODx offers to devs or end users.

                You see the thing with MODx is that I have the freedom to modify the document parser, or use an echo, or write a plugin to change the way the parser handles snippet output, or extend the parser class, or...etc...etc

                I might not like the automatic echoing, but there are multiple ways around it so I’m happy.

                Further discussion of what MODx "should" do with snippet return values is kind of pointless, though, and borders on trolling.

                BobRay: If you actually read my posts - What I was looking for was answers as to why the parser does what is does...
                  • 3749
                  • 24,544 Posts
                  Sorry if I misunderstood you. When you said:
                  IMO data ’returned’ from them [snippets] should not automatically be echoed...
                  I thought you were suggesting that we change this behavior.
                    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
                    Hi Bobray,

                    OpenGeek’s explanation of why is all I was looking for...

                    I definitely don’t think anything should be changed, everything is MODx is excellently designed/excellently coded and well thought out so if it’s there it must for a reason.

                    Thanks for all the replies smiley