I remember, that in old thema about my framework I was asked: Why do I create HTML-Markup with so complex way.
Answer is: because HTML-Markup itself could be quite complex. And if you just print it out, you should to repeat a lot of same markup parts. I think, it’s clear, this is no good.
I use, for Markup rendering two concepts. Below I will quote Readme from my framework: https://github.com/igor1999/gi
In addition to horizontal layers, we see a number of vertical rectangles
in the diagram. These rectangles are components.
A component is a collection of PHP classes and other resources located
in the same directory that is responsible for the creation and operation
of a separate frame of a web document: menu, table, list, form, etc…
Also, one component can be an aggregator for other components.
The most commonly used of these aggregators is the Layout component,
which contains the components responsible for navigation,
authorization and other things...
The component is responsible for the full cycle of functioning of its frame -
from client events processed by java-scripts to writing to persistent storage.
However, as can be seen from the diagram, classes and resources belonging
specifically to this component are in the lower two and a half layers.
Classes from the top two and a half layers are outside the components
and are called and used if required.
The GI\Component framework package contains a number of standard components
that can be used either directly or as a basis for creating your own components
needed in the current project.
The GI\DOM package contains classes for rendering XML and HTML elements.
Once we have instantiated such a class, we can configure it by setting the tag,
attributes, classes, style attributes and so on, and then using
the toString() * method to get the appropriate XML or HTML markup.
The above package contains classes that correspond to the main XML
and HTML elements, attributes, text nodes and a host of others…
For example, this package has the Layout class, which allows you to automate
the creation of a layout based on divs with the float property.
An important advantage when using such classes is the ability
to reuse markup generator classes through inheritance, composition, etc...
Therefore, when dealing with clearly structured views, such as tables, forms,
lists, etc, it is better to use DOM classes.
GI deliberately abandons the __toString() magic method. The reason is that
an exception in this method results in an error.