I'm writing a template enginge class, a simple one, my first acctually. What I want to do is to use the template class within the page generate.php.
Which template to use is decided by a querystring passed to the document: generate.php?template=1 which is the id of a template filname in my MySQL database.
In the template I use a "tag" like this:
<#mther action="METHOD" id="METHOD_ID"#>
Right now the action can be either "getData" or "doMethod".
I've comment the code pretty good I think so I'm sure you'll understand what I'm trying to do.
The problem right now is that I don't got any error msg, just a blank white page.
What's wrong, am I doing anything right at all?
Here's my code:
The template class:
<?
class processTemplate {
// Specify global variables.
var $stream;
var $stream_content;
var $open_database;
var $getData;
var $display; //object that displays the processed page
//The function that acctually process the template file.
//This function has the same name as the class, and goes off with it.
function processTemplate ($TEMPLATE,$HOST,$USER,$PSWD,$DB,$WEBROOTREF) {
//Open MySQL database
$this->open_database($HOST,$USER,$PSWD,$DB);
//get template file name by passed template ID
$query = "SELECT name FROM templates WHERE id='". $TEMPLATE ."'";
$query_result = mysql_query($query, $this->open_database);
while ($row = mysql_fetch_row($query_result)) { $template_file = $row[0]; }
//Collect template stream, this will be collected in $this->stream_content
$stream_to_open = $_SERVER["DOCUMENT_ROOT"].$WEBROOTREF."/templates/".$template_file;
$this->stream_content = $this->open_template_stream($stream_to_open);
//Check for mther tags in template document
//mther tags look like this: <#mther action="METHOD" id="IDNR"#>
$match_string = $this->stream_content;
preg_match_all("/<#mther action=\"(.*?)\" id=\"(\d*?)\"#>/i", $match_string, $matches);
// count object in array, fix so the start is 0 and also correct count after removing first object
$totalt_matches = count($matches)-2;
//remove first object in array, because it's a complete mther tag and not only the values of it
$stream_values = array_slice($matches, 1);
//loop through the values
for ($i=0; $i<$total_matches; $i++) {
if ($stream_values[$i][0]=="getData") {
$this->display = $this->getData($stream_values[i][1]);
}
else if ($stream_values[$i][0]=="doMethod") {
$this->doMethod($stream_values[i][1],$WEBROOTREF);
}
} // end for
// close open stream
$this->close_template_stream($this->stream);
$this->close_database();
}
// Function to display data to the browser if that's requested by a mther tag
function getData($ID) {
$query = "SELECT content FROM template_text_content WHERE id='". $ID ."'";
$query_result = mysql_query($query, $this->open_database) or die("Couldn't query MySQL database!". mysql_error());
while ($row = mysql_fetch_row($query_result)) { $this->getData = $row[0]; }
return $this->getData;
}
// This function includes a php function file specified by ID number in the MySQL database. These functions are written by the user
// and are uploaded in the users /functions directory
function doMethod($ID,$WEBROOTREF) {
$query = "SELECT AS realname FROM user_functions WHERE id='". $ID ."'";
$query_result = mysql_query($query, $this->open_database);
while ($row = mysql_fetch_row($query_result)) { $function_name = $row[0]; }
if (!include($_SERVER["DOCUMENT_ROOT"].$WEBROOTREF."/functions/".$function_name)) { //include the function
return false;
} else {
return true;
}
}
// Function to open template stream and to collect the stream data.
// Requires a valid template stream file with specified refer.
function open_template_stream($TEMPLATE) {
if (!$this->stream = fopen($TEMPLATE,"rb")) {
echo("Error! Couldn't open template file.\n");
exit;
} if (isset($this->stream)) {
$this->stream_content = fread($this->stream, filesize($TEMPLATE));
}
return $this->stream_content;
}
// Function to close an open template stream.
// Requires valid open template stream.
function close_template_stream($STREAM) {
fclose($STREAM);
return true;
}
// Function to open a connection to a MySQL databas.
// This function also select specified database.
// Requires HOST, USERNAME and PASSWORD.
function open_database($HOST,$USER,$PSWD,$DB) {
$this->open_database = mysql_connect($HOST,$USER,$PSWD) or Die("Couldn't open MySQL Database". mysql_error());
mysql_select_db($DB, $this->open_database);
}
// Function to close an open MySQL database handle.
function close_database() {
mysql_close($this->open_database);
}
} // end class
?>
and the generate.php file:
<?
// generate.php
// query strings requred: "template" - that's the id for the template database
require("processor/processor.class.php");
if (checkQueryStr()) {
//Specify requred variables
$template = $_GET["template"];
$host = "localhost"; //MySQL host address
$username = ""; //MySQL username
$password = ""; //MySQL password
$database = "mther"; //Database to use
$webroot = "/mther/user"; //Refering to the webroot
$processor = new processTemplate($template,$host,$username,$password,$database,$webroot);
// Display processed template page
echo($templateProcessor->display);
}
else {
echo("No template ID specified");
exit;
}
// Function to check if querystring "template" are passed to the script
function checkQueryStr() {
if (isset($_GET["template"])) {
return true;
}
else {
return false;
}
}
?>
Help me please! I really want this to get going!