Location: PHPKode > scripts > Generalized CachedTemplate class > data_valid_example1.php
<?php
/*
 * $Id: datavalid_example1.php,v 1.1 2000/07/18 21:25:45 jesus Exp $
 */

// This example demostrates the use of data validation
// when the data to be used comes from an external
// source. In this case we use the data's timestamp

require "./class.FastTemplate.php3";
require "./class.FastTemplateAdaptor.php";
require "./class.CachedTemplate.php";

$ftpl = new CachedTemplate();
$ftpl->init("./templates");

// for benchmarking
$start = $ftpl->utime();

// we need this definition first, so the new logic can
// detect if the templates have changed.
$ftpl->define(
    array(
        main    => "main.tpl",
        table   => "table.tpl",
        row     => "row.tpl"
    )
);

// get the timestamp from the data source
$data_check = filemtime("./rows.dat");

echo "<hr><b>Example on how to use the is_data_valid() method for
when the data sources are external (i.e. a file, a database, etc.). Using
the timestamp of the data file in this case: $data_check</b><hr>";

// Check if we can send the cached file
// and also check that the data is still valid
if ($ftpl->valid_cache_file() && $ftpl->is_data_valid($data_check, "timestamp")) {
    echo "<B>FROM CACHE</B>\n<BR>";
    $ftpl->read_from_cache();
    $end = $ftpl->utime();
    $runtime = ($end - $start) * 1000;
    echo "Completed in $runtime miliseconds<BR>\n";
    exit;
}

// Otherwise process the page
$ftpl->assign( array( TITLE => "FastTemplate Test") );

// Read the data from the external source, in this case a file
$rows = file("./rows.dat");

for ($i=0; $i<count($rows); $i++) {
	list($number,$bignum) = explode(",",$rows[$i]);

    $ftpl->assign(
        array(
            NUMBER      =>  $number,
            BIG_NUMBER  =>  $bignum
        )
    );

    $ftpl->parse(ROWS,".row");
}


$ftpl->parse(MAIN, array("table","main"));

// get parsed document
$data = $ftpl->getParsedDoc();
echo $data;

// for benchmarking
$end = $ftpl->utime();
$runtime = ($end - $start) * 1000;
echo "Completed in $runtime miliseconds<BR>\n";

// we write the file at the end so this
// operation will not be counted in the benchmark
// the second parameter is needed for data validation
$ftpl->write_to_cache($data, $data_check);

?>
Return current item: Generalized CachedTemplate class