Help with the Template system again

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Help with the Template system again

Post by Takuma »

Hi guys,
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.&nbsp; 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 &copy; Python 2003
		</td>
	</tr>
</table>
</body>
</html>
<?php
		exit();
	}
}
?>
The error that come is:-
Fatal error: Call to undefined function: read_template() in template.php on line 83

Please help.[/b]
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Why does PHP say that the function has not been defined even it has... ?

Any ideas please help.
redJag
Forum Newbie
Posts: 18
Joined: Fri Jan 31, 2003 12:17 am

Post by redJag »

Well I don't know what the - > operator does, but you do have double semicolons after one of the read_template calls. I don't know if that would cause that error though..seems unlikely. Depending on what the - > operator does you can only pass one argument to that function..
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

$this->TMPL, -> is used to access a variable inside a class. I'll chect the double semi-colon out.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Nope that's nothin to do with the error :cry:
Post Reply