Hey guys. I'm wondering if you can help. I followed the following tutorial on how to create a template system for your website (it's just a start for me, one day I'll know smarty):
Build a Template Parser Class
So according to the template, you should be able to view the page as long as everything goes okay. I've got it all set up, and it runs fine. But nothing is displayed. Here's some code:
/***[ index.php ]***/
<?php
session_start();
include_once($_SERVER['DOCUMENT_ROOT'].'/test_new_version/scripts/config.php');
require_once($dir['scrpt'].'parser.php');
/***[ Set up some basic Tag information ]***/
$tags = array(
'Title' => 'Winfield V.F.D. || Carroll County Station 14',
'Server_Host' => $_SERVER['HTTP_HOST'],
'Main_Content' => '',
'Events' => '',
'News' => '',
'Copyright' => 'Copyright © 2004 - 2005 Winfield V.F.D.'
);
/***[ Do a query to get the 5 most recent calls ]***/
$result = run_query('stories', '', 'id DESC', 5);
while($row = mysql_fetch_array($result))
{
$tags['Main_Content'] .= '<div class="article">
<span class="title">';
$tags['Main_Content'] .= $row['Title'].'</span>
<p class="story">';
$tags['Main_Content'] .= $row['Intro'].'<br />
<a href="http://'.$_SERVER['HTTP_HOST'].'/read.php?page=stories:'.$row['id'].'">Read More . . .</a></p>
<span class="posted">'.$row['Posted'].'</span>
</div>';
}
/***[ Do more of the same, and set other values ]***/
/***[ OMITTED FOR SPACE ]***/
#####################
/***[ Instantiate a new parster object ]***/
/***[ tags(array), cache file, expiration length (7 days), template ]***/
$tp = &new templateParser($tags, 'wcvfd_index.txt', 7*24*60*60, $dir['tpl'].'main_index.tpl');
/***[ Display the page ]***/
$tp->display();
?>
/***[ parser.php ]***/
<?php
include_once($_SERVER['DOCUMENT_ROOT'].'/test_new_version/scripts/config.php');
class templateParser
{
var $output;
function templateParser($tags = array(), $cacheFile = 'wcvfd_default.txt', $expireTime = '604800', $templatefile = '../templates/index.tpl')
{
// constructor setting up class initialization
if( !$this->output = $this->readCache($cacheFile, $expireTime) )
{
( file_exists($templatefile) ) ? $this->output = file_get_contents($templatefile) : die("<font style='color: #f00; font-weight: bold;'>Error:</font> Template file (".$templatefile.") was not found!!");
$this->parseTemplate($tags, $cacheFile);
}
}
function parseTemplate($tags = array(), $cacheFile)
{
// code for parsing template files
if(count($tags)>0)
{
foreach($tags as $tag => $data)
{
$data = (file_exists($data)) ? $this->parseFile($data) : $data;
$this->output = str_replace('{'.$tag.'}', $data, $this->output);
}
$this->writeCache($cacheFile, $this->output);
// If I uncomment, the page prints fine
// print($this->output);
}
else{
die("<font style='color: #f00; font-weight: bold;'>Error:</font> No tags were given for replacement!!");
}
}
function parseFile($file)
{
// Parse the file
ob_start();
include($file);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
function writeCache($cacheFile, $content)
{
$fp = fopen($cacheFile, 'w');
fwrite($fp, $content);
fclose($fp);
}
function readCache($cacheFile, $expiretime)
{
if(file_exists($cacheFile) && filemtime($cacheFile)>(time()-$expireTime))
{
return file_get_contents($cacheFile);
}
else
{
return FALSE;
}
}
function display()
{
// code for displaying the finished parsed page;
return $this->output;
}
}
?>
/***[ config.php ]***/
<?php
/***[ General Script Settings ]***/
$db = array(
'host' => 'localhost',
'user' => '*******',
'pass' => '*******',
'db' => 'database'
);
$dir = array(
'tpl' => $_SERVER['DOCUMENT_ROOT'].'/test_new_version/templates/',
'scrpt' => $_SERVER['DOCUMENT_ROOT'].'/test_new_version/scripts/'
);
/***[ Non-Essential Code ]***/
/***[ OMITTED FOR SPACE ]***/
?>
Any reason why the page won't render? If I print out $this->output after the replacements are done, it works. But if I take out the print statement (commented out now) then it shows a blank page.
Any ideas why this class won't work?
The tutorial uses a *.htm template, but even after changing, it doesn't work using that either.
~Brett