3 ms·
I've always thought that the problem with that viewpoint is that PHP is actually a pretty bad templating language. <?php echo htmlentities($name); ?> In D
by simonw 16y ago
I've always thought that the problem with that viewpoint is that PHP is actually a pretty bad templating language.
<?php echo htmlentities($name); ?>
In Django templates:
{{ name }}
Things get a bit better if you write your own "h" function:
function h($var) { echo htmlentities($var); }
- tcdent 16y agoThe htmlentities issue isn't anything a little upstream logic couldn't fix; just the same as Django's templating engine does. <?= $name ?> is my preferred method of printing inline, which shortens it up quite a bit.
- rix0r 16y agoMine, too, although I usually do my escaping/encoding in the template itself, so it becomes: <?= h($name) ?> Which isn't that terrible. The PHP developers don't make this easy though, as "short tags" are officially recommended to be disabled, so to write portable PHP you'd have to do <?php= h($name) ?> Every time, which adds visual noise that gets worse the more tags you write.
- owyn 16y agoI am not here to defend php (Okay, I guess I am...) but the form <?= htmlentities($name) ?> is a lot better... We have to have an i18n layer anyway for all output so it ends up being a lot of <?= Msg('message-name') ?> calls. Use some code gen macros and it's tolerable. Also, XHP seems pretty rad. It gives me ideas. :)
- pornel 16y ago`htmlentities()` defaults to ISO-8859-1 encoding, so the short form is useless in practice, and you need: <?= htmlentities($name, ENT_QUOTES, 'UTF-8'); ?>
- ElbertF 16y agoAdding an extra layer of abstraction to have somewhat nicer looking source code doesn't seem worth it though.
- Alan01252 16y agoI definitely agree. Especially when I'm passing my code to designers who are scared of code. Giving them a template file with something like {$CustomerFirstName}. Which they can then move around without fear of breaking the page makes sense. And let's be honest there aren't that many websites in the world which have to really worry about the performance implications of using smarty.
- byoung2 16y agoI'm with you there...I like that Smarty covers up some of PHP's quirks, like htmlentities($var) vs urlencode($var) becoming {$var|escape:'html'} and {$var|escape:'url'} respectively. There are also nice little shortcuts in Smarty, like wrapping templates in {strip}{/strip} to strip newlines and whitespace which would be tricky in PHP without using output buffering.