Help needed on a template system.
Posted: Wed Sep 18, 2002 3:01 pm
Hi I have made the following template system but having trouble with it. Here's the code:-
When I test it with the following code:-
but I get the following error:-
Any ideas? Please Help... 
Code: Select all
<?php
class class_template {
var $root_directory; //Root Directory
var $variables_num; //Number of variables defined
var $variables; //Name of variables defined
var $file_handle; //File handle for variables
var $file_names; //File names for variables
var $file_content; //Content of the template file
var $variable_used; //Number of times that the varible has been processed
function tpl_define($root_directory,$template_variables) {
//Check whether the specified root directory is a diretory or not
if(@is_dir($root_directory)) {
$this->root_directory = $root_directory;
} else {
$this->error_report("The specified root directory, "$root_directory", is not a directory!");
exit();
}
$this->variables_num = 0;
while(list($variable_name,$variable_file) = each($template_variables)) {
//Check whether the template file exist in the root directory or not
if(!@file_exists($this->root_directory."/".$variable_file)) {
$this->error_report("The specified template file, "$variable_file", does not exist under the root directory, "{$this->root_directory}".");
exit();
}
$this->file_handleї$this->variables_num] = @fopen($this->root_directory."/".$variable_file,"r") or die($this->error_report("Fail to open file, "$variable_file", under the root directory, "{$this->root_directory}""));
$this->variablesї$this->variables_num] = $variable_name;
$this->file_namesї$this->variable_num] = $variable_file;
$this->variable_num++;
}
}
function tpl_assign($variable,$value) {
if(substr($variable,0,1) != ".") {
for($i = 0; $i <= $this->variable_num; $i++) {
$this->file_contentї$i] = fread($this->file_handleї$i],filesize($this->root_directory."/".$this->file_namesї$i]));
if(strlen(trim($this->file_contentї$i])) == 0) {
$this->error_report("Could not read a template file!");
exit();
}
if(strstr($file_contentї$i], "{".$variable."}")) {
$this->file_contentї$i] = str_replace("{".$variable."}",$value,$file_contentї$i]);
}
}
} else {
if($this->variable_usedї$variable] == 0) {
for($i = 0; $i <= $this->variable_num; $i++) {
$this->file_contentї$i] = fread($this->file_handleї$i],@filesize($this->file_namesї$i]));
if(strlen(trim($this->file_contentї$i])) == 0) {
$this->error_report("Could not read template file!");
exit();
}
if(strstr($file_contentї$i], "{".$variable."}")) {
$this->variable_usedї$variable]++;
$this->file_contentї$i] = str_replace("{".$variable."}",$value,$file_contentї$i]);
}
}
} else {
for($i = 0; $i <= $this->variable_num; $i++) {
$this->file_contentї$i] = @fread($this->file_handleї$i],@filesize($this->file_namesї$i]));
if(strlen(trim($this->file_content)) == 0) {
error_report("Could not read template file!");
exit();
}
$start_position = strpos("{".$variable."}",$file_content);
$string_length = strlen("{".$variable."}");
$end_position = $start_position + $string_length + 1;
$first_string = substr($file_contentї$i],0,$start_position);
$second_string = substr($file_contentї$i],$start_position + 1,$end_position);
$file_contentї$i] = $first_string + "{" + $variable + "}".$second_string;
if(strstr($file_contentї$i], "{".$variable."}")) {
$this->variable_usedї$variable]++;
$this->file_contentї$i] = str_replace("{".$variable."}",$value,$file_contentї$i]);
}
}
}
}
}
function tpl_print($template_variables) {
$template_variables = explode(",",$template_variables);
$template_variables_num = count($template_variables);
for($i = 0; $i <= $template_variables_num; $i++) {
for($n = 0; $n <= $this->variable_num; $n++) {
if($template_variablesї$i] == $this->variablesї$n]) {
echo $filecontentї$n];
}
}
}
}
function error_report($error_msg) {
echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n
<html>\n
<head>\n
<title>Unexpected error has occured</title>\n
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\n
</head>\n
<body>\n
<div align="center"> <span style="font-family: Verdana, Arial, Helvetica, sans-serif; color: #FF0000; font-size: 12px; font-weight: bold;">$error_msg<br>\n
<a href="javascript:opener.back()">Click here to go back.</a></span>\n
</div>\n
</body>\n
</html>";
}
}
?>Code: Select all
<?php
include("library/template.php");
$tpl = new class_template();
$tpl->tpl_define("library",array("basic" => "tpl.tpl"));
$tpl->tpl_assign("HI","HEY");
$tpl->tpl_assign("BYE","ADHASKDJ");
$tpl->tpl_print("basic");
?>Code: Select all
Warning: fread(): supplied argument is not a valid File-Handle resource in /usr/home/ticktaku/public_html/library/template.php on line 50
Could not read a template file!
Click here to go back.