Here's what I got till but I have a problem...
<?php
/*
Template Function Library
*/
class class_template {
var $root_folder; //Stores root folder of template files
var $file_content; //Stores the content of tempalte files
var $filenames; //Stores the names of template files
var $variables; //File Handle for the template file
var $key; //Stores the variable for tempalte
var $num;
function define($folder,$template_variables) {
if(is_dir($folder)) {
$this->root_folder = $folder;
} else {
$this->error_report("The root directory specified is not a directory!");
exit();
}
$this->num = 0;
while(list($key,$value) = each($template_variables)) {
if(!file_exists($this->root_folder."/".$value)) {
$this->error_report("$value does not exists!");
exit();
}
$this->filenames[$i] = $value;
$this->variables[$i] = fopen($this->root_folder."/".$value,"r");
$this->key[$i] = $key;
$this->num++;
}
}
function assign($template_vairable,$value) {
for($i = 0; $i != $this->num; $i++) {echo $this->filenames[$i];;
$this->file_content[$i] = fread($this->vairables[$i],filesize($this->filenames[$i]));
$this->file_content[$i] = str_replace("{$template_vairable}",$value,$this->file_content);
}
unset($i);
}
function print_print($order) {
$order = explode(",",$order);
$order_num = count($order);
$i = 0; $n = 0;
while($i != $this->num) {
while($n != $this->num) {
if($order[$i] == $file_content[$n]) {
echo $file_content[$n];
$n = 0;
return false;
}
}
}
}
function error_report($error_msg) {
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
<head>
<title>Unexpected error has occured</title>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
</head>
<body>
<div align=\"center\"> <span style=\"font-family: Verdana, Arial, Helvetica, sans-serif; color: #FF0000; font-size: 12px; font-weight: bold;\">".$error_msg."</span>
</div>
</body>
</html>";
}
}
?>
in the function "assign" it tries to access $this->filename and $this->variable but it seems that it's empty. I tried accessing it on function "define" and it holds a value. This is causing the following error when I execute with the following code. The code above is called template.php and under a folder called "library"
<?php
include("library/template.php");
$tpl = new class_template();
$tpl->define("library",array("basic" => "tpl.tpl"));
$tpl->assign("CONTENT","hey");
?>
When I execute this I get this error:-
Warning: stat failed for (errno=2 - No such file or directory) in /usr/home/ticktaku/public_html/library/template.php on line 43
Warning: fread(): supplied argument is not a valid File-Handle resource in /usr/home/ticktaku/public_html/library/template.php on line 43
Now I have not got any idea why $this->filenames and $this->variables becomes empty... Please help :!: