WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : CachedFastTemplate class, extends CDI's FastTemplate to allow page caching
Categories : HTML and PHP, HTML, PHP Classes Update Picture
Jesus M. Castagnetto
Date : Feb 01st 2000
Grade : 1 of 5 (graded 1 times)
Viewed : 3684
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Jesus M. Castagnetto
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

===== CachedFastTemplate class =====

<?php

/*
* Class CachedFastTemplate
* by Jesus M. Castagnetto (jesusmc@scripps.edu)
* (c) 2000. Version 1.0
*
* Description:
* This class extends CDI's (cdi@thewebmasters.net) 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;

?>



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
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
class formHTML build your HTML Forms from PHP
Categories : PHP, PHP Classes, HTML and PHP, HTML
Display Slashdot headers on your own site
Categories : HTML and PHP, HTML, PHP
Sed, a coder's best friend. This lovely linux command allows you to swap strings in a file. Great for changing a variable name in a script with multiple files.
Categories : HTML and PHP, HTML and PHP
webcam cam view image ispy browser independant
Categories : Graphics, HTML, HTML and PHP, PHP
Browser Detecor Class
Categories : PHP Classes, PHP, HTML
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
Class to build a select tag in html, useful to build select boxes from a data base
Categories : PHP, HTML and PHP, PHP Classes
How to preset a text string in a textarea input field
Categories : HTML, HTML and PHP, PHP, Beginner Guides
phpMyDataGrid 2007
Categories : AJAX, PHP Classes, MySQL, Databases, HTML and PHP
This script shows you the 7th latest php items from the mailing list archive on zend.com
Categories : HTML, HTML and PHP, HTTP, PHP
Snipe.Net's Web Design Color Scheme Previewer- Allows uses to input hex codes for their text, background, and link colors, and preview the color scheme with their background image. Example: http://www.snipe.net/tech/snipeschool/hex.php3
Categories : PHP, HTML and PHP, General, Graphics, HTML
How To Create a PDF Using PHP
Categories : PHP, PDF, PHP Classes, HTML and PHP
GonxTabs : Create elegant HTML tabs based interface
Categories : Navigation, HTML, HTML and PHP, PHP