Help needed on a template system.

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 needed on a template system.

Post by Takuma »

Hi I have made the following template system but having trouble with it. Here's the code:-

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>";
		}
	}
?>
When I test it with the following code:-

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");
?>
but I get the following error:-

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.
Any ideas? Please Help... :cry:
User avatar
~J~R~R
Forum Newbie
Posts: 20
Joined: Wed Sep 18, 2002 12:19 pm
Location: Amsterdam, the Netherlanda

Post by ~J~R~R »

This means $this->file_handle[$i] isn't a valid File-Handle resource. IE the file doesn't exist or the variable isn't stored correct. Are you sure the file exists and $i is an correct index? Use error_reporting(E_ALL); to see notices about undefined variables.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Yes I am sure...

Code: Select all

<?php
 $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}"")); 
?>
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Have you tried removing the "@" in that line and seeing what message it gives?

Also have you tried doing a print_r($tpl) right before you call print to see if the class looks the way you think it should?
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

I have tried removing "@". And the 2nd suggestion I'll give it a try.! :)
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Replace this:

Code: Select all

for($i = 0; $i <= $this->variable_num; $i++) &#123;
with this:

Code: Select all

for($i = 0; $i < $this->variable_num; $i++) &#123;
On lines 37 and 50
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

OK I've got it kind of working... But the problem now is

Code: Select all

&lt;?php
strlen(trim($this-&gt;file_content&#1111;$i]))
?&gt;
this returns 95''0 and when I do this

Code: Select all

&lt;?php
if(strlen(trim($this-&gt;file_content&#1111;$i])) == 0) {
?&gt;
It's returns false... Any ideas?


Code is as follows:-

Code: Select all

&lt;?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-&gt;root_directory = $root_directory;
			} else {
				$this-&gt;error_report("The specified root directory, "$root_directory", is not a directory!");
				exit();
			}

			$this-&gt;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-&gt;root_directory."/".$variable_file)) {
					$this-&gt;error_report("The specified template file, "$variable_file", does not exist under the root directory, "{$this-&gt;root_directory}".");
					exit();
				}

				$this-&gt;file_handle&#1111;$this-&gt;variables_num]	= fopen($this-&gt;root_directory."/".$variable_file,"r") or die($this-&gt;error_report("Could not open file, "$variable_file" under the root directory, "{$this-&gt;root_directory}""));
				$this-&gt;variables&#1111;$this-&gt;variables_num]		= $variable_name;
				$this-&gt;file_names&#1111;$this-&gt;variable_num]		= $variable_file;
				$this-&gt;variable_num++;
			}
		}

		function tpl_assign($variable,$value) {
			if(substr($variable,0,1) != ".") {
				for($i = 0; $i &lt; $this-&gt;variable_num; $i++) {
					$this-&gt;file_content&#1111;$i] = fread($this-&gt;file_handle&#1111;$i],filesize($this-&gt;root_directory."/".$this-&gt;file_names&#1111;$i]));
echo "'".strlen(trim($this-&gt;file_content&#1111;$i]))."'";
					if(strlen(trim($this-&gt;file_content&#1111;$i])) == 0) {
						$this-&gt;error_report("Could not read a template file!");
						exit();
					}
					if(strstr($file_content&#1111;$i], "{".$variable."}")) {
						$this-&gt;file_content&#1111;$i] = str_replace("{".$variable."}",$value,$file_content&#1111;$i]);
					}
				}
			} else {
				if($this-&gt;variable_used&#1111;$variable] == 0) {
					for($i = 0; $i &lt; $this-&gt;variable_num; $i++) {
						$this-&gt;file_content&#1111;$i] = fread($this-&gt;file_handle&#1111;$i],filesize($this-&gt;root_directory."/".$this-&gt;file_names&#1111;$i]));
						if(strlen(trim($this-&gt;file_content&#1111;$i])) == 0) {
							$this-&gt;error_report("Could not read template file!");
							exit();
						}
						if(strstr($file_content&#1111;$i], "{".$variable."}")) {
							$this-&gt;variable_used&#1111;$variable]++;
							$this-&gt;file_content&#1111;$i] = str_replace("{".$variable."}",$value,$file_content&#1111;$i]);
						}
					}
				} else {
					for($i = 0; $i &lt; $this-&gt;variable_num; $i++) {
						$this-&gt;file_content&#1111;$i] = fread($this-&gt;file_handle&#1111;$i],filesize($this-&gt;file_names&#1111;$i]));
						if(strlen(trim($this-&gt;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&#1111;$i],0,$start_position);
						$second_string		= substr($file_content&#1111;$i],$start_position + 1,$end_position);
						$file_content&#1111;$i]	= $first_string + "{" + $variable + "}".$second_string;
						if(strstr($file_content&#1111;$i], "{".$variable."}")) {
							$this-&gt;variable_used&#1111;$variable]++;
							$this-&gt;file_content&#1111;$i] = str_replace("{".$variable."}",$value,$file_content&#1111;$i]);
						}
					}
				}
			}
		}

		function tpl_print($template_variables) {
			$template_variables					= explode(",",$template_variables);
			$template_variables_num	= count($template_variables);
			for($i = 0; $i &lt;= $template_variables_num; $i++) {
				for($n = 0; $n &lt;= $this-&gt;variable_num; $n++) {
					if($template_variables&#1111;$i] == $this-&gt;variables&#1111;$n]) {
						echo $filecontent&#1111;$n];
					}
				}
			}
		}

		function error_report($error_msg) {
			echo "&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;\n
&lt;html&gt;\n
  &lt;head&gt;\n
	 &lt;title&gt;Unexpected error has occured&lt;/title&gt;\n
	 &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;\n
  &lt;/head&gt;\n
  &lt;body&gt;\n
	 &lt;div align="center"&gt; &lt;span style="font-family: Verdana, Arial, Helvetica, sans-serif; color: #FF0000; font-size: 12px; font-weight: bold;"&gt;$error_msg&lt;br&gt;\n
		&lt;a href="javascript:opener.back()"&gt;Click here to go back.&lt;/a&gt;&lt;/span&gt;\n
	 &lt;/div&gt;\n
  &lt;/body&gt;\n
&lt;/html&gt;";
		}
	}
?&gt;
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

I take it you guys don't know... :cry:
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

In your earlier post you said it returned 95"0, is it 0 or 95? 95 == 0 would yield false.
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Thanks nielsene, I've manage to solve that problem but i've got a new problem... which is making me go crazy!! Now this template system is made that so if the user calles 2 function tpl_assign and gave a "." before the variable it means that the script will duplicate the string {CONTENT} and then replace that as well.

Code: Select all

&lt;?php
tpl_assign(".CONTENT","HEY");
tpl_assign(".CONTENT","BYE");
?&gt;
And if you have the folowing template

Code: Select all

&lt;html&gt;
&lt;head&gt;&lt;title&gt;Hello&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&#123;CONTENT&#125;
&lt;/body&gt;
&lt;/html&gt;
It should print out like

Code: Select all

&lt;html&gt;
&lt;head&gt;&lt;title&gt;Hello&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
HEYBYE
&lt;/body&gt;
&lt;/html&gt;
But it doesn't do that. Here's the new code...

Code: Select all

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-&gt;root_directory = $root_directory;
			} else {
				$this-&gt;error_report("The specified root directory, "$root_directory", is not a directory!");
				exit();
			}

			$this-&gt;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-&gt;root_directory."/".$variable_file)) {
					$this-&gt;error_report("The specified template file, "$variable_file", does not exist under the root directory, "{$this-&gt;root_directory}".");
					exit();
				}

				$this-&gt;file_handle&#1111;$this-&gt;variables_num]	= fopen($this-&gt;root_directory."/".$variable_file,"r") or die($this-&gt;error_report("Could not open file, "$variable_file" under the root directory, "{$this-&gt;root_directory}""));
				$this-&gt;variables&#1111;$this-&gt;variables_num]		= $variable_name;
				$this-&gt;file_names&#1111;$this-&gt;variables_num]		= $variable_file;
				$this-&gt;variable_num++;
			}
		}

		function tpl_assign($variable,$value) {
			if(substr($variable,0,1) != ".") {
				for($i = 0; $i &lt; $this-&gt;variables_num; $i++) {
					$this-&gt;file_content&#1111;$i] = fread($this-&gt;file_handle&#1111;$i],filesize($this-&gt;root_directory."/".$this-&gt;file_names&#1111;$i]));
					if(strlen(trim($this-&gt;file_content&#1111;$i])) == 0) {
						$this-&gt;error_report("Could not read a template file!");
						exit();
					}
					$this-&gt;file_content&#1111;$i]  = str_replace("{".$variable."}",$value,$this-&gt;file_content&#1111;$i]);
				}
			} else {
				if($this-&gt;variable_used&#1111;$variable] == 0) {
					for($i = 0; $i &lt;= $this-&gt;variables_num; $i++) {
						$this-&gt;file_content&#1111;$i] = fread($this-&gt;file_handle&#1111;$i],filesize($this-&gt;root_directory."/".$this-&gt;file_names&#1111;$i]));
						if(strlen(trim($this-&gt;file_content&#1111;$i])) == 0) {
							$this-&gt;error_report("Could not read a template file!");
							exit();
						}
						$this-&gt;variable_used&#1111;$variable]++;
						$new_variable = substr($variable,1,strlen($variable));
						$this-&gt;file_content&#1111;$i]  = str_replace("{".$new_variable."}",$value,$this-&gt;file_content&#1111;$i]);
					}
				} else {
					for($i = 0; $i &lt;= $this-&gt;variables_num; $i++) {
					//This bit isn't working!!!!!
						$this-&gt;file_content&#1111;$i] = fread($this-&gt;file_handle&#1111;$i],filesize($this-&gt;root_directory."/".$this-&gt;file_names&#1111;$i]));
						if(strlen(trim($this-&gt;file_content&#1111;$i])) == 0) {
							$this-&gt;error_report("Could not read a template file!");
							exit();
						}
						$start_position		= strpos("{".$variable."}",$file_content);echo $start_position;
						$string_length		= strlen("{".$variable."}");
						$end_position		= $start_position + $string_length + 1;
						$first_string		= substr($file_content&#1111;$i],0,$start_position);
						$second_string		= substr($file_content&#1111;$i],$start_position + 1,$end_position);
						$file_content&#1111;$i]	= $first_string + "{" + $variable + "}".$second_string;
						if(strstr($file_content&#1111;$i], "{".$variable."}")) {
							$this-&gt;variable_used&#1111;$variable]++;
							$this-&gt;file_content&#1111;$i] = str_replace("{".$variable."}",$value,$file_content&#1111;$i]);
						}
					}
				}
			}
		}

		function tpl_print($template_variables) {
			$template_variables					= explode(",",$template_variables);
			$template_variables_num	= count($template_variables);
			$i = 0; $n = 0;
			while($i &lt; $template_variables_num) {
				while($n &lt; $this-&gt;variable_num) {
					if($this-&gt;variables&#1111;$n] == $template_variables&#1111;$i]) {
						echo $this-&gt;file_content&#1111;$n];
					}
					$n++;
				}
				$i++;
			}
		}

		function error_report($error_msg) {
			echo "&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;\n
&lt;html&gt;\n
  &lt;head&gt;\n
	 &lt;title&gt;Unexpected error has occured&lt;/title&gt;\n
	 &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;\n
  &lt;/head&gt;\n
  &lt;body&gt;\n
	 &lt;div align="center"&gt; &lt;span style="font-family: Verdana, Arial, Helvetica, sans-serif; color: #FF0000; font-size: 12px; font-weight: bold;"&gt;$error_msg&lt;br&gt;\n
		&lt;a href="javascript:opener.back()"&gt;Click here to go back.&lt;/a&gt;&lt;/span&gt;\n
	 &lt;/div&gt;\n
  &lt;/body&gt;\n
&lt;/html&gt;";
		}
	}
Thanks again in advance... :wink:
Post Reply