PHP Classes
Icontem

File: datavalid_example2.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Jesus M. Castagnetto  >  Generalized CachedTemplate class  >  datavalid_example2.php  
File: datavalid_example2.php
Role: ???
Content type: text/plain
Description: example of using external data validation using an md5 hash
Class: Generalized CachedTemplate class
General Template Caching class
 

Contents

Class file image Download
<?php
/*
 * $Id: datavalid_example2.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 MD5 hash


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"
    )
);


// using the md5 hash of the external data
$data_check = md5(implode("", file("./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 MD5 hash of the data: $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, "md5")) {
    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);

?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products