However, this isn??™t enabled by default. To enable it, you need
to enclose the processing instructions with an if conditional and evaluate the
is_cached() method, like this:
require("Smarty.class.php");
$smarty = new Smarty;
$smarty->caching = 1;
if (!$smarty->is_cached("lottery.tpl")) {
if (date('l') == "Tuesday") {
$random = rand(100000,999999);
}
}
$smarty->display("lottery.tpl");
?>
In this example, the lottery.tpl template will first be verified as valid. If it is, the
costly database access will be skipped. Otherwise, it will be executed.
Creating Multiple Caches per Template
Any given Smarty template might be used to provide a common interface for an entire
series of tutorials, news items, blog entries, and the like. Because the same template is
used to render any number of distinct items, how can you go about caching multiple
instances of a template? The answer is actually easier than you might think. Smarty??™s
developers have actually resolved the problem for you by allowing you to assign a
unique identifier to each instance of a cached template via the display() method. For
example, suppose that you want to cache each instance of the template used to render
professional boxers??™ biographies:
require("Smarty.class.php");
require("boxer.class.php");
500 CHAPTER 19 ?– T EMPLAT I NG WITH SMARTY
$smarty = new Smarty;
$smarty->caching = 1;
try {
// If template not already cached, retrieve the appropriate information.
Pages:
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581