|
|
===== CachedFastTemplate class =====
<?php
/*
* Class CachedFastTemplate
* by Jesus M. Castagnetto ([email protected])
* (c) 2000. Version 1.0
*
* Description:
* This class extends CDI's ([email protected]) FastTemplate class,
* implementing methods to cache the output to a file. This would be
* useful for cases in which a template is used for both, the static
* pages in a web site, and pages created from PHP scripts. In this
* way we will avoid processing pages that do not change too often.
*/
class CachedFastTemplate extends FastTemplate {
var $CACHEDIR = "./cache"; // directory to save cached files
var $CACHELENGTH = 30; // length of caching, 30 days.
function CachedFastTemplate($path_to_tpls="", $cdir="", $clen="") {
$this->FastTemplate($path_to_tpls);
if (!empty($cdir)) {
$this->set_cache_dir($cdir);
}
if (!empty($clen)) {
$this->set_cache_length($clen);
}
}
function set_cache_dir($dir) {
if (substr($dir,(strlen($dir) - 1), 1) == "/") {
$dir = substr($dir,0,-1);
}
$this->CACHEDIR = $dir;
}
function set_cache_length($length) {
$this->CACHELENGTH = $length;
}
function write_to_cache($filename="", $content="") {
global $PHP_SELF;
if (empty($filename)) {
$filename = str_replace("/", "_", $PHP_SELF);
}
if (empty($content)) {
$content = $this->fetch();
}
// write the contents
$fp = fopen($this->CACHEDIR."/".$filename.".cache", "w");
fwrite($fp,$content);
fclose($fp);
// write the cache control file
$datestamp = $this->_mkdatestamp();
$fp = fopen($this->CACHEDIR."/".$filename.".cntrl", "w");
fwrite($fp, $datestamp.":".$this->CACHELENGTH);
fclose($fp);
}
function read_from_cache($filename="") {
global $PHP_SELF;
if (empty($filename)) {
$filename = str_replace("/", "_", $PHP_SELF);
}
if ($this->is_cached($filename)) {
readfile($this->CACHEDIR."/".$filename.".cache");
return true;
} else {
return false;
}
}
function is_cached($filename) {
return (is_file($this->CACHEDIR."/".$filename.".cache") &&
is_file($this->CACHEDIR."/".$filename.".cntrl"));
}
function valid_cache_file($filename="") {
global $PHP_SELF;
if (empty($filename)) {
$filename = str_replace("/", "_", $PHP_SELF);
}
if ($this->is_cached($filename)) {
$info = file($this->CACHEDIR."/".$filename.".cntrl");
$val = explode(":", $info[0]);
$today = $this->_mkdatestamp();
return ( $this->_diff_days($today, $val[0]) <= $val[1] );
} else {
return false;
}
}
function _mkdatestamp() {
$now = getdate(time());
return mktime(0, 0, 0, $now["mon"], $now["mday"], $now["year"]);
}
function _diff_days($end, $start) {
return intval(($end - $start)/(60*60*24));
}
} // end of class definition
?>
===== test_class.php =====
<?php
// This test file is a simple modification of a
// sample file include in FastTemplate
require ("./class.FastTemplate.php3");
require ("./class.CachedFastTemplate.php3");
$tpl = new CachedFastTemplate("./templates");
// for benchmarking
// $start = $tpl->utime();
$filename = "yipeee.html";
// if the filename is empty, the class will use PHP_SELF
// Check if we can send the cached file
if ($tpl->valid_cache_file($filename)) {
echo "<B>FROM CACHE</B>\n<BR>";
$tpl->read_from_cache($filename);
$end = $tpl->utime();
$runtime = ($end - $start)*1000;
echo "Completed in $runtime miliseconds<BR>\n";
exit;
// Otherwise process the page
// the templates are distributed w/FastTemplate
$tpl->define(
array(
main => "main.tpl",
table => "table.tpl",
row => "row.tpl"
)
);
$tpl->assign( array( TITLE => "FastTemplate Test") );
for ($n=1; $n <= 3; $n++)
{
$Number = $n;
$BigNum = $n*10;
$tpl->assign(
array(
NUMBER => $Number,
BIG_NUMBER => $BigNum
)
);
$tpl->parse(ROWS,".row");
}
$tpl->parse(MAIN, array("table","main"));
$data = $tpl->fetch();
$tpl->write_to_cache($filename, $data);
$tpl->FastPrint();
// for benchmarking
// $end = $tpl->utime();
// $runtime = ($end - $start)*1000;
// echo "Completed in $runtime miliseconds<BR>\n";
exit;
?>
|
|
| class formHTML build your HTML Forms from PHP Categories : PHP, PHP Classes, HTML and PHP, HTML | | | Automatic generation of HTML code for a table. OO interface. Can define colspan, rowspan, table style, cell style, and data style. Simple, but
effective. Categories : PHP, PHP Classes, HTML, HTML and PHP | | | A simple class with some HTML output functions that would come in handy for consistent page layout etc. Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation | | | phpFormGenerator for Dynamic Form Generation from MySQL Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP | | | GonxTabs : Create elegant HTML tabs based interface Categories : Navigation, HTML, HTML and PHP, PHP | | | Popup Menu 0.5, popup, select, html, state-maintaing Categories : HTML, PHP, HTML and PHP | | | Class to build a select tag in html, useful to build select boxes from a data base Categories : PHP, HTML and PHP, PHP Classes | | | EditPHP - PHP HTML online file editor with encrypted password authentication. Categories : HTML and PHP, PHP Classes, PHP Options and Info | | | Display Slashdot headers on your own site Categories : HTML and PHP, HTML, PHP | | | Form is a utility class for generating html forms. It provides form initialization and regex based data validation (both server and client side) with a convenient interface. This version obsoletes version 1.0a Categories : HTML, PHP, PHP Classes, Regexps | | | Football News Aggregator Categories : PHP, Object Oriented, PHP Classes, Rich Site Summary (RSS), HTML and PHP | | | How to use regular expressions to get the list of links from an HTML page Categories : PHP, Regexps, HTML, HTML and PHP | | | How to preset a text string in a textarea input field Categories : HTML, HTML and PHP, PHP, Beginner Guides | | | Vote-Poll script that has a wrapper class that allows the user to create
multiple polls on the same page with little trouble. Categories : PHP, PHP Classes, HTML and PHP | | | A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP | |
| |
| |
|