by FriedrichN · 2y ago · 0 comments
It works great, it's fast and you don't have to learn a new templating language or dialect every 2-3 years. Or try to figure out how to do trivial things like loops in loops.
I don't quite get the use of templating engines in PHP because PHP is a templating engine itself.
[1] It's so much worse than this... You can use some horrible hacks like undefining a well-known function that each template implements, but this would only work one level deep without also adding a mechanism for "calling" a template, and at that point you are making a template system on top of PHP.
$_SERVER, $_POST, $_GET.
The rest is not the PHP3 I recognize to be honest, let alone the modern stuff. Remember include() literally just included the code right there, inline, so you could include in functions and gain the function's scope:
function render($data) {
switch ($data['foo']) {
case 'bar': include('bar.inc'); break;
case 'baz': include('baz.inc'); break;
}
}
That's your template router with all the template's data pre-loaded in the template's scope, be it ever so filthy.> Remember include() literally just included the code right there, inline, so you could include in functions and gain the function's scope:
I've talked about this approach elsewhere in thread, it is not nestable without building a way to "call" templates on top of require/include, and when you do that you'll need to undefined the render function to do it as well, which creates additional complexity. At that point, the original premise of just using raw PHP isn't very true, because you are now building a framework or templating system around it.
Also, you have to contend with the magic "render" function not being globally defined by something. It's messy.
Um. Can you give me an example to pad that out a bit?
> and requires you to do cleanup after each template instantiation to avoid leaking values between templates
In my render() example, isn't that "reaching the end of the function"? (Edit: Wait, you're only talking about the superglobals, right?)
I hear this here, and elsewhere I hear that PHP has evolved into a decent language. Both can't be true.
Namespaces allow non-global functions without a wrapping class already, IIRC. Looking up though, there seem to be special cases (legacy behavior, making everything at the top level global) when using namespaced files with include() statemtents, if I read the docs correctly.
Dumping pure functions as static members into wrapper classes was not uncommon when I used PHP.
But primarily, a lot of code was heavy on dependency injection and inversion of control, Symfony being the leader of this coding style maybe.
TFA also seems to be about class-oriented programming, but not the heavy design-pattern style mentioned above.
The linked TUI CLI parser looks like a nice and lean PHP OOP example.
$closure = function(){};
instead of
function closure(){}
1. It's a loaded foot gun. PHP was notorious for 1000 line files with HTML and SQL all mixed together. (I speak from personal experiences on this)
2. Templating languages are "portable" (In theory). Have a mixed env of PHP and Java... there were templating languages that would work for both teams, it took a step out of scalability.... XML and XSLT were the promise land on this one, but we fucked all that up. Portability is a big deal. We're still slinging CSV files for a good reason.
Improper use of templating engines or trying to use templating engines that aren't up to the task can give you headaches just as well. Sadly no amount of frameworks or templating engines can stop a bad programmer from writing bad code, in the end we're craftsmen who need to learn how to use our tools.
2. This could be a valid use case, but a rather rare one. XML+XSLT is something that sounds fantastic on paper, but as anyone who actually worked with it knows it ranges from a big disappointment to an absolute nightmare.
It's not a matter of whether the code is bad or not, but how bad.
The entire point of frameworks is to add guard rails to help you stay disciplined and avoid cutting corners on separation of concerns.
Using PHP as a template language tempts developers to violate SOC every day.
Also I don't see how using a loop or conditional in PHP is any different from using one in a templating engine, aside from the overhead of the engine and added annoyance of debugging another language in your project.
At another job, another guy built some massive abstract-factory-factory-layer in Python, a class hierarchy 4 or 5 layers deep. 5000+ lines in a single file. It was almost impossible to debug.
Both of these are better than some C++ code I once saw. 10,000+ lines in a single file. In that file, there was a function with a 3000+ line case statement, which was mostly copy-pasta'd from other parts of the code (not in that 10K line file.)
I have seen plenty of PHP code that smelled like a nuclear dumpster fire, but you can write trash in any language.
Do you write safe templates in PHP ?