Hi, i created my own template, but i an error in it. The error is:
Fatal error: Call to undefined function: file_parser() in c:\phpdev\www\public\daalhof\templates\template.tpl on line 16
This is the code of the template:
//Register a few variables
$template->register_variables("home", "page_title,bg_color");
$template->file_parser("home"); //line 16
//output the file to the browser
$template->print_file("home");
?>
If it's not clear here is:
#------------------#
| template.class |
#------------------#
<?
class template {
VAR $files = array();
VAR $variables = array();
VAR $opening_escape = '{';
VAR $closing_escape = '}';
//Function: register_file()
//Purpose: Store contents of file specified $file_id
function register_file($file_id, $file_name) {
// Open $file_name for reading, or exit and print an error message.
$fh = fopen($file_name, "r") or die("Couldn't open $file_name!");
// Read in the entire contents of $file_name.
$file_contents = fread($fh, filesize($file_name));
//Assign these contents to a position in the array.
// This position in denoted by the key $file_id
$this->files[$file_id] = $file_contents;
// We're finished with the file, so close it.
fclose($fh);
} //end register_file;
//funtion: register_variables()
//purpose: Store variables passed in via $variable_name under the corresponding
//array key, specified by $file_id
function register_variables($file_id, $variable_name) {
//attempt to create array from passed in variable names
$input_variables = explode(",",$variable_name);
//$input variable name to next position in $file_id array
while (list(,$value) = each($input_variables));
//assign the value to a new position in the $this->variables array
$this->variables[$file_id][] = $value;
}
} //end regiter_variables
// Function: file_parser()
// Purpose: Parse all registered variables in file contents
// speified by input parameter $file_id
function file_parser($file_id) {
//How many variables are registered for this particular file?
$varcount = count($this ->variables[file_id]);
//How many files are registered?
$keys = array_keys($this->files);
//If the $file_id exists in te $this->files array and it
// has some registered variables...
if ( (in_array($file_id, $keys)) && ($varcount > 0)):
//reset $x
$x = 0;
// while the are still variables to parse...
While ($x < sizeof($this->variables[$file_id])):
//retrive the next variable
$string = $this->variables[$file_id][$x];
//retrive this variable value! notice that i'm using a
// Variable variable to retrive this value. This value
// will be subsstiles into the file contenst, taking the plase
//of the corresponding variable
//name
GLOBAL $$string;
//What exactly is to be replaced in the file constants?
$needle = $this->opening_escape.$string.$this->closing_escape;
//perform the string replacement.
$this->file[$file_id] = str_replace(
$needle, //needle
$$string, //string
$this->files[$file_id]); //haystack
//increment $x
$x++;
endwhile;
endif;
}//end file_parser
// Function: print_file()
//Purpose: Print out the file contents specified by input parameter $file_Id
function print_file($file_id){
//Print out the contents pointed to by $file_id
print $this->files[$file_id];
}// END template.class
?>
and here is
#---------------#
| template.tpl |
#---------------#
<?
include("template.class");
//assign a few variables
$page_title = "Welkom op winkelcentrum-daalhof.nl";
$bg_color = "white";
//Instantiate a new object
$template = new template;
//register the file index, assigning the pseudonym "home"
$template->register_file("home", "index.php");
//Register a few variables
$template->register_variables("home", "page_title,bg_color");
$template->file_parser("home");
//output the file to the browser
$template->print_file("home");
?>
Can somebody help me out??
Cheers Stefan