PHP's Smarty releases version 3.0
groups.google.com
groups.google.com
Back when php was relatively new (2002 - 2003), our company hired a few php developers to build several ecommerce solutions. They swore up and down at the time that smarty and pear would get the job done faster and better.
Long story short, after implementing the solution rather quickly we spent more money deleting giant gobs of lame templating code and re-writing the rest of code in very lean php than we did on the original project.
Never again will I use any templating engine on top of a scripting language. It's actually a written core requirement for any new project I contract ("no lame templating engines").
Templating junk can also make it more difficult to get lean standards-compliant html that you need for reliable javascript behavior.
Don't use templating to junk up your code. You've been warned.
But have a look at http://PHPTAL.org — its attribute-only syntax not only preserves structure of HTML well, but PHPTAL actually parses XHTML and enforces its well-formedness (i.e. you can't produce HTML with misnested or unclosed tags).
One benefit of Smarty is that it forces at least some seperation of business and display logic. People will still try and shove business logic into the templates, but at least you know you're not going to find SQL statements halfway though the rendering of a table.
I don't understand all the hate against Smarty. How would the "PHP is already a templating language" argument explain the rise of all these PHP frameworks we have no, almost all of which contain some means of templating.
Smarty allows me to not have to worry about boring presentation layer crap (ie htmlentities) without having to commit to a large framework. Plus its syntax is pretty nice.
http://www.php.net/~derick/meeting-notes.html#remove-support...
Unless you have a more recent source.
Could you elaborate? What happened to Smarty during those years?
One of the core arguments against it, I think, was PHP developers suddenly remembering short tags and alternative syntaxes for control structures (if, foreach) that made working with templates in straight PHP just as easy, much more flexible, and shaving a few milliseconds off load times -- it's a sizeable library to load.
<?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); }<?= $name ?> is my preferred method of printing inline, which shortens it up quite a bit.
<?= 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.<?= 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. :)
<?= htmlentities($name, ENT_QUOTES, 'UTF-8'); ?>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.
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.
But aside of all of that, the 3rd version of Smarty puts it back on the right track.
<?php
foreach ($users as $user) {
$userinput = htmlentities($user['userinput']);
echo <<<EOD
<div>
ID: {$user['id']} <br />
Name: {$user['user_name']} <br />
Input: $userinput
</div>
EOD;
}
?>
I've used Smarty in the past though, and I didn't mind it. <? foreach ($users as $user): ?>
<div>
ID: <?=$user['id']?> <br />
Name: <?=$user['user_name']?> <br />
Input: <?=htmlentities($user['userinput'])?> <br />
</div>
<? endforeach; ?>
That said, I know I'm INSTANTLY going to piss off the "NEVER USE SHORT TAGS EVER" camp, but if it's on a server I control (which is 99% of the time), or I can modify PHP init settings via .htaccess or ini_set (which is the other 1% of the time), it literally doesn't matter.The main takeaway is the alternate control structure (foreach: endforeach; instead of foreach {}). I find that way cleaner than heredocs.
I think PHP and Perl got it right when they allowed embedding of normal vars in strings without any special syntax thanks to the $ sigil, we should take advantage of it where possible. When you have a bunch of non-PHP I think it makes sense to drop out into normal HTML, but otherwise, PHP strings are fine and your editor should support syntaxifying them as HTML.
Most PHP programmers say that it's not a big deal, as one can simply insert `h()` in every echo… but when I review PHP code, I find XSS everywhere. You can't just rely on self-discipline. It doesn't work.
Recently, I really like the look of Moustache Logic-less templates - http://mustache.github.com/ - yet to try it though.