3 ms·
There are a lot of choices to not write HTML by concatenating strings though.
by emodendroket 9y ago
There are a lot of choices to not write HTML by concatenating strings though.
- krapp 9y agoAre there in PHP, though?
- grzm 9y agoPHP does have a history of making this more explicit, given its origins as a templating language. If you go down far enough, isn't all HTML rendering string concatenation? There are plenty of libraries that handle this in many languages, including PHP.
- krapp 9y agoI'm just annoyed that PHP doesn't handle HTML tags natively as their own distinct entities. Not having to quote HTML as strings or switch into and out the rendering context with PHP tags makes templates a lot easier to reason about and cleaner. With that, you can natively get features like auto-escaping, HTML tag validation, extending or creating custom tags, template inheretance, all of which you can do in PHP but only if you include the overhead of a framework.
- grzm 9y ago> I'm just annoyed that PHP doesn't handle HTML tags natively as their own distinct entities. I'm interested in things like this as well. Can you provide an example of what you're thinking of? Something like JSX? Other data structures that are transformed into HTML by a library? The latter seems to be counter to your argument about requiring the use of some framework or library, but I can't think of any alternatives off the top of my head.
- krapp 9y agoXHP templates in Hack do wind up looking a lot like JSX (which is probably not coincidental).. Each tag is a class with a render function that returns an XML node. Here is an example base document template[0] showing what that looks like. Obviously, PHP templates can accomplish the same thing, it just takes more work and more dependencies to magic away the noise. But with XML as a native data structure you can do things like typehint a tag: public function setTitle(:document:base $base, string $title): void { $base->setAttribute("pagetitle", $title); } [0]https://pastebin.com/m54VDwNY https://pastebin.com/m54VDwNY
- grzm 9y agoThanks for the example!
- emodendroket 9y agoI've used Twig, which seems pretty comparable to similar products in other ecosystems, but there are several alternatives. So, to answer your question, yes.
- jawngee 9y agoTwig, Smarty, Blade
- krapp 9y agoAll of which just wind up building and echoing strings in the end.
- emodendroket 9y agoI mean, OK, but that's how any templating system works. I assumed the issue was not having to write string-munging code yourself.
- krapp 9y agoNo, my only issue is that PHP doesn't let you just echo the HTML directly, because despite being designed for generating HTML, the language doesn't actually know what it is.
- emodendroket 9y agoRaw PHP is far from its roots as a templating tool and is a full featured programming language. I don't really see any reason why you'd want to use it for templates.
- krapp 9y agoIt's not that far. PHP is much better as a language than it used to be, but it's still a hypertext preprocessor (they can retcon the acronym but the language is still designed around taking requests and sending responses), and templating is still almost exclusively what it's used for, because that's what it's best at.
- emodendroket 9y agoIf nobody is doing general-purpose programming (or at least Web programming) with PHP why are there three popular templating libraries?
- thatonechad 9y agoJust use a templating system like Twig