#native_company# #native_desc#
#native_cta#

5 Popular PHP Template Engines Worth Checking Out Page 2

By W. Jason Gilmore
on April 7, 2011

4. Savant3 PHP Template Engine

Savant3 differs from the other templating solutions introduced in this article in that it does not require users to learn a new syntax, instead using PHP as the templating language. You might wonder whether this defeats the stated purpose of separating PHP logic from the website’s presentation, however by distilling the PHP code down to a few presentation-specific methods you are effectively accomplishing the same goals set forth by the competing solutions, while simultaneously saving yourself the trouble of learning an engine-specific syntax. For example the following PHP script will ready a template for outputting an array of video games:

require_once 'Savant3.php';

$template = new Savant3;

$games = array (
  array (
    'title' => 'Call of Duty: Black Ops'.
    'esrb'  => 'Mature'
  ),
  array (
    'title' => 'LittleBigPlanet 2'.
    'esrb'  => 'Everyone'
  )
);

$template->games = $games;

$template->display('games.tpl.php');

Next define the template (games.tpl.php):

<?php foreach ($this->books AS $key => $value) { ?>
<div>
<h3><?php echo $this->eprint($value['title']); ?></h3>
<p>
ESRB: <?php echo $this->eprint($value['esrb']); ?>
</p>
</div>
<?php } ?>

This syntax probably looks quite familiar, save for perhaps the reference to the eprint() method. This method is a Savant3 method which will automatically escape the output before inserting it into the template, lessening the possibility of cross-site scripting attacks.
Learn more about Savant3 at phpsavant.com.

5. PHPTal PHP Template Engine

PHPTal is a PHP-based implementation of Zope Page Templates, a Web page generation tool used by the Python-based Web application server Zope. PHPTal differs from the other templating engines discussed in this article in that it implements an XML-based approach. For instance, the following PHPTal template is used to iterate over an array:

<div tal:repeat="game games">
<h3 tal:replace="game/getTitle">
<p tal:replace="game/getDescription">
</div>

Executing this template produces output which looks like this:

<div>
<h3>Call of Duty: Black Ops</h3>
<p>
The latest release in the bestselling first-person shooter series.
</p>
</div>
...
<div>
<h3>Pac-Man Championship Edition DX</h3>
<p>
Pac-Man Championship Edition DX introduces new twists and challenges into
a game we've grown to love.
</p>
</div>

Learn more about PHPTal at phptal.org.

Conclusion

Are you using a PHP templating engine not included in this list? Tell us about it in the comments!

About the Author

Jason Gilmore is founder of the publishing, training, and consulting firm WJGilmore.com. He is the author of several popular books, including “Easy PHP Websites with the Zend Framework”, “Easy PayPal with PHP”, and “Beginning PHP and MySQL, Fourth Edition”. Follow him on Twitter at @wjgilmore.