Hardhat Framework

Why I Like Smarty Templates

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:

Smarty
  First Name: {$name.firstName}
  Last Name: {$name.lastName}
   <ul>
  {$foreach $members as $member}
    <li>{$member->getLastName()}, {$member->getFirstName()}</li>
  {foreachelse}
    <li>There were no members</li>
  {/foreach}
    </ul>
PHP
  First Name: <?php=name['firstName'] ?>
  Last Name: <?php=name['lastName'] ?>
   <ul>
  <?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 } ?> 
    </ul>

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.

Performance

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.