On 15/08/2004, at 7:02 AM, Octavian Rasnita wrote:

Hi all,

I have seen that there are many templating systems for PHP. Which is the
most used and the best you have found?
Can you recommend me a free and good templating system?

Ultimately it depends on who is going to be building those templates. Is it a HTML programmer, or a Dreamweaver monkey, or is it you (a PHP programmer)?


It's a very old argument that has been done to death on this list 100's of times, but PHP *is a templating language* itself.

<h1><?=$title?></h1>
<table>
        <thead>
                <td>Name</td>
                <td>Email</td>
        </thead>
        <? foreach($users as $user): ?>
        <tr>
                <td><?=$user['name']?></td>
                <td><?=$user['email']?></td>
        </tr>
        <? endforeach; ?>
</table>

Personally, I prefer to just use PHP wherever possible. The reality is that Smarty et al all compile or parse these templates into PHP at some point, so my question is, why not just use PHP? You'll eventually run into limitations with any other template language ("you can't do that with smarty").


However, templating languages are PERFECT in an environment where the programmer (you) doesn't want to give the template designer (someone else) the full power of PHP.


Others have (and probably will again) argue that the templating languages like Smarty are easier for the designer to learn, but I completely disagree. I can't see any benefits -- check out the following examples:

{$name|capitalize}
{$smarty.now|date_format:"%Y-%m-%d"}
<table>
{section name=mysec loop=$users}
{strip}
   <tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}">
      <td>{$users[mysec].name}</td>
      <td>{$users[mysec].phone}</td>
   </tr>
{/strip}
{/section}
</table>

These all look just as simple and easy in PHP:

<?=capitalize($name)?>
<?=date('Y-m-d')?>
<table>
<? foreach($users as $user): ?>
   <tr bgcolor="<?=cycle("#aaaaaa","#bbbbbb")?>">
      <td><?=$user['name']?></td>
      <td><?=$user['phone']?></td>
   </tr>
<? endforeach; ?>
</table>


Yes, I'd have to have a library of various functions like capitalize() and cycle(), but this is all rudimentary stuff that could be achieved very quickly -- in fact, I've already got most of them written.



Now, to pre-empt a flame by many of Smarty's fans, Smarty does offer many other advantages (caching, restricted power, encourages separation of code and presentation, etc).


As I said right back at the start, it depends on who the target author of the templates will be, and what skills they have. I've spent a lot of time in Smarty, Textpattern's XHTML based template language and many more, and in most cases *I* would prefer straight PHP, but you need to spend a little time with them and decide for yourself, based on your own needs.



---
Justin French
http://indent.com.au

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to