People credit PHP’s ability to blend-in with HTML as one of the most important reason for it’s popularity. So I contemplated it as a templating language today.
Turns out, PHP is not anywhere near an impressive templating language after all.
To me, the deal breaker is its inability to achieve template inheritance, something Python web developers take granted for. In Jinja2 or Django’s templating language, a template can “implement” blocks declared in its parent template, whose code is shared among its children. To the controllers, this mechanism is completely hidden, so they simply use those “child” templates to render everything, as if they are full-fledged templates.
With PHP, intuitively, similar functionality can be done with ‘include’ & co. You can take one of two approaches:
- Top-down. Use a variable in place of the content of the “child” template, determine which child template is need and pass it to the parent template.
- Bottom-up. Break up the repeated (“parent”) code and include them as needed in the actual content. Of course you’ll need at least 2 “include”s (a header and a footer) in the simplest case
Either way, you get one level of “inheritance” done.
To be fair though, a library is all that’s need to bring PHP back into the game.