Why I Like Smarty

Clean Separation of Concerns

Smarty helps you keep business logic out of the view. Whenever you find yourself wondering how to make smarty do something, it probably means you are trying to do it in the wrong place. It is probably something that would be more appropriate in the model or possibly even the controller logic.

Easy to Read

Smarty really helps your HTML look like HTML. Compare the following code samples:

  First Name: {$name.firstName}
  Last Name: {$name.lastName}
  {$foreach $members as $member}
    <li>{$member->getLastName()}, {$member->getFirstName()}</li>
    <li>There were no members</li>
  First Name: <?php=name['firstName'] ?>
  Last Name: <?php=name['lastName'] ?>
  <?php foreach($members as $member) { ?>
    <li> <?php=$member->getLastName()?>, <?php=$member->getFirstName()?></li>
  <?php }  
  if (count($members) == 0) { ?>
    <li>There were no members</li>
  <?php } ?> 

Sure you could use short tags, but that comes with it's own set of problems.

Notice also how difficult it is to keep the braces straight. Smarty just makes all of this so much easier to deal with.

Notice also how the php code can span multiple lines. This is almost never the case in a Smarty template.


You may be concerned about performance. It is true that smarty templates must be compiled, but they are compiled once and then cached as native php code.