<html>
<head><title>cheeses</title></head>
<body>
<table border="1">
<tr>
<th>cheese</th>
<th>country</th>
<th>price</th>
</tr>
{section name=id loop=$results}
<tr>
<td>{$results[id]->cheese}</td>
<td>{$results[id]->country}</td>
<td>{$results[id]->price}</td>
</tr>
{/section}
</table>
</body>
</html>
Here's the corresponding PHP file that loads the
data from the database and then displays the template, stored in
food.tpl:
require 'Smarty.class.php';
mysql_connect('localhost','test','test');
mysql_select_db('test');
$r = mysql_query('SELECT * FROM cheese');
while ($ob = mysql_fetch_object($r)) {
$ob->price = sprintf('$%.02f',$ob->price);
$results[] = $ob;
}
$smarty = new Smarty;
$smarty->assign('results',$results);
$smarty->display('food.tpl');
After including the base class for the templating engine
(Smarty.class.php), you retrieve and format the
results from the database and store them in an array. To generate the
templated page, just instantiate a new $smarty
object, tell $smarty to pay attention to the
$results variable, and then tell
$smarty to display the template.