Prev | Current Page 570 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"


if (!is_cached("boxerbio.tpl", $_GET['boxerid'])) {
$bx = new boxer();
if (! $bx->retrieveBoxer($_GET['boxerid']) )
throw new Exception("Boxer not found.");
// Create the appropriate Smarty variables
$smarty->assign("name", $bx->getName());
$smarty->assign("bio", $bx->getBio());
}
/* Render the template, caching it and assigning it the name
* represented by $_GET['boxerid']. If already cached, then
* retrieve that cached template
*/
$smarty->display("boxerbio.tpl", $_GET['boxerid']);
} catch (Exception $e) {
echo $e->getMessage();
}
?>
In particular, take note of this line:
$smarty->display("boxerbio.tpl", $_GET['boxerid']);
This line serves double duty for the script, both retrieving the cached version of
boxerbio.tpl named $_GET["boxerid"], and caching that particular template rendering
under that name if it doesn??™t already exist. Working in this fashion, you can easily cache
any number of versions of a given template.
CHAPTER 19 ?–  TEMPLAT ING WITH SMARTY 501
Some Final Words About Caching
Template caching will indeed greatly improve your application??™s performance and
should seriously be considered if you??™ve decided to incorporate Smarty into your
project. However, because most powerful Web applications derive their power from
their dynamic nature, you??™ll need to balance these performance gains with the cached
page??™s relevance as time progresses.


Pages:
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582