I’ve recently been exposed to a PHP practice of only using a single echo statement. So effectively the view is built in a view class and a function exists to return the HTML.
This is then echoed, with this being the only echo statement in the page:
$output_html = $obj_cars_view->get_output_html();
echo $output_html;
N.B cars was the first real life object I thought of and is unrelated, I’m more interested in what advantages people can identify to using this and seeing whether its worth me adopting this method.
If the only advantage is readability and its not being widely used then its counter productive as its only more readable to people who are familiar with the practice.
It makes it easier to do post-processing on the output, even if just inspection. That’s possible with php’s output buffering, but this is a more direct approach.
It’s easier to reuse/repurpose code if output goes into a buffer instead of directly to the page. Again, built-in output-buffering can do some of this, but it’s not as flexible.
Conceptually I think you can argue that a “single output” model is cleaner that something that urps out bits of html here and there, but as you note, it does come at a cost.