Smarty Templates
Smarty templates are primarily HTML pages that have special keywords loaded that the Smarty processor evaluates into a cache at run time.
Here is a sample PHP and template directly from http://smarty.php.net
<?php
include('Smarty.class.php');
// create object
$smarty = new Smarty;
// assign some content. This would typically come from
// a database or other source, but we'll use static
// values for the purpose of this example.
$smarty->assign('name', 'george smith');
$smarty->assign('address', '45th & Harris');
// display it
$smarty->display('index.tpl');
?>
Smarty Template
<?php
<html>
<head>
<title>User Info</title>
</head>
<body>
User Information:<p>
Name: {$name}
Address: {$address}
</body>
</html>
?>
Discussion of Smarty
The $smarty object is really a wrapper to a whole set of underlying Smarty libraries available to you. assign() places named values into an array. display() takes the template mentioned, substitutes values for fields that mactch previous assignments and generates the corresponding HTML page to the browser.
Savant Templates
Savant templates are primarily HTML pages that standard PHP tags embedded. The PHP code in the template references magic variable names per the Savant naming convention.
Here is a sample PHP and template directly from http://phpsavant.com
<?php
// Load the Savant2 class file and create an instance.
require_once 'Savant2.php';
$tpl =& new Savant2();
// Create a title.
$name = "Some Of My Favorite Books";
// Generate an array of book authors and titles.
$booklist = array(
array(
'author' => 'Hernando de Soto',
'title' => 'The Mystery of Capitalism'
),
array(
'author' => 'Neal Stephenson',
'title' => 'Cryptonomicon'
),
array(
'author' => 'Milton Friedman',
'title' => 'Free to Choose'
)
);
// Assign values to the Savant instance.
$tpl->assign('title', $name);
$tpl->assign('books', $booklist);
// Display a template using the assigned values.
$tpl->display('books.tpl.php');
?>
Savant Template
<?php
<html>
<head>
<title><?php $this->_($this->title) ?></title>
</head>
<body>
<?php if (is_array($this->books)): ?>
<!-- A table of some books. -->
<table>
<tr>
<th>Author</th>
<th>Title</th>
</tr>
<?php foreach ($this->books as $key => $val): ?>
<tr>
<td><?php echo $this->_($val['author']) ?></td>
<td><?php echo $this->_($val['title']) ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php else: ?>
<p>There are no books to display.</p>
<?php endif; ?>
</body>
</html>
?>
Discussion of Savant
In the PHP code we assign name/value pairs. Notice that you really have embedded PHP code in your template. Savant gives you a standard way to pass variables into your templates.
Template It Templates
Template It templates are HTML pages that have specially named tags that Template It converts and replaces at run time with values provided by the PHP code.
Here is a sample PHP and template directly from http://pear.php.net/manual/en/package.html.html-template-it.intro.php
<?php
require_once "HTML/Template/IT.php";
$data = array
(
"0" => array("Stig", "Bakken"),
"1" => array("Martin", "Jansen"),
"2" => array("Alexander", "Merz")
);
$tpl = new HTML_Template_IT("./templates");
$tpl->loadTemplatefile("main.tpl.htm", true, true);
foreach($data as $name) {
foreach($name as $cell) {
// Assign data to the inner block
$tpl->setCurrentBlock("cell") ;
$tpl->setVariable("DATA", $cell) ;
$tpl->parseCurrentBlock("cell") ;
}
// parse outter block
$tpl->parse("row");
}
// print the output
$tpl->show();
?>
Template It Template
<?php
<html>
<table border>
<!-- BEGIN row -->
<tr>
<!-- BEGIN cell -->
<td>
{DATA}
</td>
<!-- END cell -->
</tr>
<!-- END row -->
</table>
</html>
?>
Discussion of Template It
In Template It the PHP code executes in a lock-step correspondence with the components of the HTML page being generated.
Overall
My estimate is that Smarty is the clear leader in the field for PHP templating. There is a clear delineation between the code and the template being used.
Savant appears to offer a cleaner way for PHP developers to get into templating. However, I dislike the inclusion of actual PHP code in the templates. It seems to go against the standard MVC model.
Template It does not appear to offer any advantage over the others. The template constructs are complicated. This may appeal to Pear fanatics. |