I'm making a new version of template system but got a problem... I don't know why it occurs, well here's the code:-
Code: Select all
<?php
class class_template {
var $ROOT; //Root direcotry
var $TMPL; //Template variable and filename
var $REPLACE; //String to be replaced
var $CONTENT; //Parsed template
//Set root directory
function set_root($directory) {
if( !(is_dir($directory)) ) {
$this->error("Root directory, '$directory', is not a directory.");
}
if($directory == "/") {
$directory = "";
}
$this->ROOT = $directory;
}
//Set template files
function set_template($templates) {
foreach($templates as $key => $value) {
if($this->ROOT == "") {
$folder = $value;
} else {
$folder = $this->ROOT."/".$value;
}
if( !(file_exists($folder)) ) {
$this->error("Template, '$value', does not exist in the root directory.");
}
$this->TMPL[$key] = $value;
}
}
//Assign template variables to be parsed
function assign($variable, $string) {
settype($variable, "string");
settype($string, "string");
$this->REPLACE[$variable] = $string;
}
//Replaces the actual template variables into a new string
function replace($template, $replace) {
$i = 0;
foreach($replace as $key => $value) {
if( substr($key, 0,1) == ".") {
$this->STORED[$i][$key]++;
$this->STORED_KEY[$i] = $key;
$this->STORED_VALUE[$i] = $value;
} else {
$template = ereg_replace("\\\{$key\\}", $value, $template);
}
$i++;
}
if( (count($this->STORED) != 0) ) {
$num = count($this->STORED);
for($i = 0; $i < $num; $i++) {
$key = $this->STORED_key[$i];
$value = $this->STORED_VALUE[$i];
for($n = 0; $n < $this->STORED[$i][$key]; $n++) {
$value .= $value;
}
}
$template = ereg_replace("\\\{$key\\}", $value, $template);
}
return $template;
}
//Reads the data from the template file
function read_template($template) {
$content = implode(" ",@file($this->ROOT.$template));
if( empty($content) || $content ) {
$this->error("Could not read from the template file.");
}
return $content;
}
//Control the parsing of the template
function parse($result, $templates) {
if( !(is_array($templates)) ) {
if( (empty($this->TMPL[$templates])) ) {
$this->error("'$templates' has not been defined.");
}
$content = read_template($this->TMPL[$templates]);;
$content = replace($content, $this->REPLACE);
} else {
foreach($templates as $key => $value) {
if( (empty($this->TMPL[$templates])) ) {
$this->error("'$templates' has not been defined.");
}
$content = read_template($this->TMPL[$templates]);
$content = replace($content, $this->REPLACE);
}
}
$this->CONTENT[$templates] = $content;
}
function print_tpl($result, $templates) {
foreach($temptlaes as $key => $value) {
foreach($this->TMPL as $key_2 => $value_2) {
if($key == $key_2) {
echo $this->CONTENT[$key];
}
}
}
}
//Used when printing out error messages
function error($message) {
?>
<html>
<head>
<title>Unexpected Error has Occured!</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table width="70%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle" style="color: #FF0000; font-weight: bold; border: 1px solid #000066; background: #000066;">
Unexpected Error
</td>
</tr>
<tr>
<td style="border: 1px solid #000066; border-top: none; border-bottom: none;">
There was an error while preparing the page. The following
error message has been returned:-<br>
<span align="center" style="color: #FF0000;"><?php echo $message; ?></span>
</td>
</tr>
<tr>
<td align="center" valign="middle" style="color: #FFFFFF; border: 1px solid #000066; background: #000066;">
Copyright © Python 2003
</td>
</tr>
</table>
</body>
</html>
<?php
exit();
}
}
?>Fatal error: Call to undefined function: read_template() in template.php on line 83
Please help.[/b]