Reputation: 1697
The documentation on snippets in modx: http://rtfm.modx.com/display/revolution20/Snippets
Near the top of the doc it says: Note how we returned the code rather than echo'ed the content out. Never use echo in a Snippet - always return the output.
This doesn't display anything:
return $product_attribute $product_info[0][$column_name];
This does display:
echo $product_attribute $product_info[0][$column_name];
If I can't echo the content how do I get it to print in the html page?
Upvotes: 0
Views: 1127
Reputation:
It basically means that you can echo
the returned
value rather than echo
the value in the function itself. In OOP programming, echoing (or printing) to the screen is strictly monitored.
For example I have this function
function testExample($var) {
return $var*2;
}
So when I need to echo it, I just need to
echo testExample(5);
Instead of this (bad practice)
function testExample($var) {
echo $var*2;
}
The reason is that when you print value in the function, you can only use that function for printing the value, which isn't reusable at all. But by returning it, you can now use it for printing, or assigning to another variable or re-calculating on it.
Upvotes: 3
Reputation: 46620
return
is used to return values from a function/method ect
The snippet
functionality of modx is actually just a function/class wrapper.
Upvotes: 0
Reputation: 121
From what I can understand from the note, there is a convention not to use echo
inside a function, but rather return the value and perhaps echo it afterwards.
Other printing posibilities would be:
print $your_variable;
or die($your_variable);
Upvotes: 0