3 ms·
why should a PHP app use a templating engine when PHP itself is one? It's just cleaner, and makes it easier, visually, to separate layout from code. Wordpress
by krapp 13y ago
why should a PHP app use a templating engine when PHP itself is one?
It's just cleaner, and makes it easier, visually, to separate layout from code. Wordpress seems to be a good example of the opposite case -- it switches directly between PHP and html and there are no 'templates' to speak of, in the way Smarty/Twig etc. people would think of them. It is 'closer to the metal' I guess but also for anyone who's had to edit a theme, really ugly.
Personally I find just not having to worry about context with opening and closing PHP tags and echoing variables (and manually escaping everything) worth the extra bit of abstraction involved with a templating system, which will do the work for me.
- Udo 13y ago> It's just cleaner, and makes it easier, visually, to separate layout from code. That's true, and I think PHP would be better for it if there were three kinds of files by convention: pure code, layout, and entry points that are actually callable (the other two shouldn't be invokable by typing in their URL). It's trivial to configure a webserver to handle them differently, but it's impossible if you're writing code that needs to run everywhere. However, I would not say that {$blah} is that much more readable than <?= $blah ?>, not to the extent that the complexity and cost of running a separate engine is justified.
- krapp 13y agoDepending on how the code is structured, it might not matter much either way in terms of legibility (assuming the sight of something like if($foo){ ?> <b> <?php echo $bar; ?></b> <?php } doesn't set your teeth on edge to begin with), but I do think the former is easier to type and maintain which in PHP to me counts for a lot. Then again I never use the short tags so i'm adding at least those three characters of overhead per variable anyway. Also, having the views inline couples them to whatever logic they happen to be in, so the templates are less portable. A lot of templating engines will meet you halfway and compile down to classes so they don't reinterpret the templates each time. YMMV I guess.
- Udo 13y agoYes, but if you're using short tags, things can be better ;) <? if($foo) { ?> <b><?= $bar ?></b> <? } ?> True, it's not as nice as a templating language, but then again, it doesn't have the aforementioned limitations either. Inlining views is a choice, it tend to avoid this. Just because templating engines prevent this pattern doesn't mean you'll be forced to use it in plain PHP. Re-interpreting templates is also an interesting case. Of course, native templates will pretty much always be faster so the need to cache isn't as pressing. But still, with the built-in output buffer functions it's easy to do something like do_some_caching(function() { include('sometemplate'); });
- krapp 13y agoI was playing around with Slim framework recently and I 'discovered' the option in the PDO driver to fetch into a class. Add a parent class to the data classes (with magic setters and getters, having the getter wrap everything in htmlspecialchars)[1] and you really can, probably, accomplish most of what a lot of templating systems do (and a good chunk of MVC with the serial numbers filed off) with a fraction of the code in most frameworks and, say, Twig. Even static caching probably, using output buffering. [1]http://stackoverflow.com/questions/8898794/using-pdofetch-class-with-magic-methods http://stackoverflow.com/questions/8898794/using-pdofetch-cl...