4 ms·
All I want is a templating language in the WordPress core and I can live with all the other bad parts.
by sdnguyen90 12y ago
All I want is a templating language in the WordPress core and I can live with all the other bad parts.
- jacques_chester 12y agoWe could call it "Personal Hypertext Processor". It'll be great.
- krapp 12y agoThe meme that PHP is a templating language may be technically accurate, but as a templating language, raw PHP is so terrible that the thought of using it as such should also be dismissed outright. There is nothing great about this sort of thing: <?php for($i=0; $i<$num_rows; $i++) { ?> <div><?php echo htmlspecialchars($db_row[$i]['field']); ?></div> <?php } ?> repeated ad-nauseum across hundreds of files. And then somebody decides to drop php variables directly into javascript and css... No. No no no.
- mappu 12y agoYou forgot `ENT_QUOTES | ENT_SUBSTITUTE` on `htmlspecialchars`, it's perilous to use directly in every single place. But after embracing `short_open_tag` (or `<?=` is always available in 5.4+), foreach, `\PDO::FETCH_OBJ` and a helper function; <?php foreach($row in $results) { ?> <div><?=h($row->field)?></div> <?php } ?> Is it really longer/worse than the equivalent twig/smarty/handlebars ?
- krapp 12y ago>You forgot `ENT_QUOTES | ENT_SUBSTITUTE` on `htmlspecialchars`, it's perilous to use directly in every single place. I would have added it in real code... but manually escaping everything is still what you have to do with raw php. At the very least, have something to recursively escape an array (keys and values) before it gets dumped onto the page. Twig at least lets you escape by default (and you don't have to worry about double-escaping), but the equivalent wouldn't be much less verbose. Twig's syntax is a bit cleaner in couple of places I think, and it lets you use array bracket syntax [] in versions of PHP too old to support it natively. {% for row in results %} <div>{{ row.field }}</div> {% endfor %} Of course this doesn't include the added complexity of an entire framework implementing its own language running on top of everything but still, the environment is a bit more sane.
- whichdan 12y agoMeh. With short tags, and a helper function which can be abstracted away in your stack, you can write the following: <? foreach($row in $results): ?> <div><?= $row['field'] ?></div> <? endforeach ?> (Assume the helper function maps htmlspecialchars over an array returned by PDO::FETCH_ASSOC.)
- krapp 12y agoAt that point, though, once you no longer have to echo and escape everything, you have a templating framework on top of PHP. The number and complexity of 'helper functions' differs between this and something like Twig, but it's still using abstractions to manage what PHP doesn't do well on its own. A lot of self-described minimalist templating languages do wind up looking a lot like that. I personally don't want to see raw PHP in anything i'm calling a template but to each their own.
- TeMPOraL 12y agoWell, it's still strictly better than all the templating engines used in PHP for... well, no good reason. You have equivalent, or sometimes even shorter, syntax in PHP, and with templating engines, it always ends up with developers working around engine's limitation in some ways. Why use something underpowered for the task where you could use an actual programming language?