Why is it considered bad to use echo in a snippet?
What advantage does return have over echo?
Many thanks in advance.
Glyn Szasz
Sydney, Australia
Happy to help (when can) and happy to learn
If you are a Sydney based MODX developer would love to hear from you. Please contact me.
-
☆ A M B ☆
- 24,524 Posts
For one thing, using print() or echo can have the output inserted in unexpected places in your page.
For another thing, except for the most trivial of snippets, a snippet should use a form of user-configurable templating for its output. Its output should be loaded into placeholders, then a defined template, a chunk, a file, or inline snippet property, should be used to replace the placeholder tags in the template with the appropriate values from the snippet output.
Basically you should treat a snippet or plugin as you would treat a function or a method in any well-designed code.
https://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/snippets/how-to-write-a-good-snippet
echo would be used for testing in a snippet. the snippet must return true, this tells modx that the snippet run sucessfully. I would probibly advise using print_r() for testing anything in your snippet, but still include the return true and the end of the snippet.