Right now i have a template class that builds up cache files, it is based on a tutorial that i have been modifying to better accommodate the project i am working on.
The issue is in the index.php file, what i want it to do is only make a call to the database if ether the cache file does not exist or the cache has expired.
Now this is kinda working just now but every now and then i get a error message from the templateParser that no tags were sent, this could only occur on the '/* do not connect to database, use existing cache file instead. */' statement, i am unsure how to rectify this though i assume it is my lack of experience in the working with cacheing pages.
In the future i am going to look into making the cache files as XML as my lectures at Uni talk quite fondly of this but i have yet to get into how these work and that is a problem for another day.
templateParser.php
index.php
The issue is in the index.php file, what i want it to do is only make a call to the database if ether the cache file does not exist or the cache has expired.
Now this is kinda working just now but every now and then i get a error message from the templateParser that no tags were sent, this could only occur on the '/* do not connect to database, use existing cache file instead. */' statement, i am unsure how to rectify this though i assume it is my lack of experience in the working with cacheing pages.
In the future i am going to look into making the cache files as XML as my lectures at Uni talk quite fondly of this but i have yet to get into how these work and that is a problem for another day.
templateParser.php
PHP:
<?php
/*
templateParser
KittenBunny 2.0
Jerry Roy
*/
class templateParser
{
/*
Set the output variable
*/
public $output;
/*
function to set the template and cache file
*/
public function templateParser($tags=array(),$cacheFile='default_cache.txt',$expireTime=3600, $templateFile='default_template.htm')
{
if(!$this->output=$this->readCache($cacheFile,$expireTime))
{
(file_exists($templateFile))?$this->output=file_get_contents($templateFile)
:die('Error:Template file '.$templateFile.' not found');
$this->parseTemplate($tags,$cacheFile);
}
}
/*
Function to sent the content to the template
*/
public function parseTemplate($tags,$cacheFile)
{
if(count($tags)>0)
{
foreach($tags as $tag=>$data)
{
$data=(file_exists($data))?$this->parseFile($data):$data;
// this is the cached version
$this->output=str_replace('{'.$tag.'}',$data,$this->output);
}
// cached
$this->writeCache($cacheFile,$this->output);
}
else
{
die('Error: No tags or files were provided for replacement');
}
}
/*
Parse's the file
*/
public function parseFile($file)
{
ob_start();
include($file);
$content=ob_get_contents();
ob_end_clean();
return $content;
}
/*
Writes the cache file
*/
public function writeCache($cacheFile,$content)
{
$fp=fopen($cacheFile,'w');
fwrite($fp,$content);
fclose($fp);
}
/*
Read the cache File
*/
public function readCache($cacheFile,$expireTime)
{
if (file_exists($cacheFile) && filemtime($cacheFile)>(time()-$expireTime))
{
return file_get_contents($cacheFile);
}
return false;
}
/*
Return the populated template
*/
public function display()
{
return $this->output;
}
}
?>
index.php
PHP:
try
{
/* Check if a cache file is ready made */
if (file_exists($cacheFileIndex))
{
/* do not connect to database, use existing cache file instead. */
}
else
{
/* Creates a new cache file and displays the output from the cache file */
/* Connect to the table */
$task = "dbmysql";
$query_select = "select `body`, `header` from `bootstrap`";
$database_name = "testcms";
/* Initialise the database */
$dbC = new DbControl($task, "cp1250");
$dbC->selectDb($database_name);
$dbC->setQuery($query_select);
$dbR = $dbC->initiate();
/* Prepair the data for writing the new cache file */
$output=array
(
'header'=>$dbR->get("header"),
'body'=>$dbR->get("body")
);
}
/* Write the new data into the cache file */
$output =&new templateParser($output,$cacheFileIndex,10,'../application/modules/bootstrap/views/IndexView.tpl.htm');
/* Displays the output from the cache file */
echo $output->display();
}
catch (Exception $e) {
echo '<p>A database connection error was found. Please try again later.</p>';
}