Page 1 of 1

Need some help with a template class

Posted: Thu Nov 08, 2007 2:22 pm
by Andromeda
Hello out there, i have used a template class for a long time now.
But here reasons have i desided to expand it a bit, with a new more html linke way for defining the variabels in the html, and as a whole new thing, has i added validators.

But my javascript for the validators is not wroking as inteded, they don't worke at all :D but they should after my javascript knowlage.
Furtermore, would i like some help, with what can be done smarter/faster/sexyer in my class.

The things that most conserns me about this class it
1: The java for the validators
2: The way i extract the html'tags from the template files
3: The way i paste the vars, is the a way that can be done faster?


I have created a little demo, that shows almost all it functions.
See the output here: http://www.androme.dk/phpdn/1/index.php

The code for the index.php file:

Code: Select all

<?PHP

require ("class.fasttemplate.php");

$tp = new FastTemplate("./");

// Loading the template..
// You can load as many templates as you want, and merge them together at will
$tp->load(array('main' => "template.html"));

// Testing oure variabels
$tp->paste('main', array('v1' => "Variabel 1 workes", 'v2' => "Variabel 2 workes"));

// Testing the loops
$tp->setloop('main', 'testloop');
$tp->looppaste('loop.testloop', array( '0' => array('loopvar' => 1), '1' => array('loopvar' => 2), '2' => array('loopvar' => 3)));
$tp->macromerge('main', 'testloop');

// The empty loop
$tp->setloop('main', 'testloop2');
$tp->looppaste('loop.testloop2', array());
$tp->macromerge('main', 'testloop2');


// Testing the if's
$tp->tif('main', 'iftest1', true);
$tp->tif('main', 'iftest2', false);


$tp->techo('main', true); // YOU MUST ONLY VALIDATE ON THE LAST ECHO, OTHERWISE WON'T IT WORKE!!!

?>

The template.html file:

Code: Select all

<html>
<head>
	<title>Test Page</title>
</head>
<body>

	<h1>Variable test</h1><br />
	Variabel 1: $v1<br />
	Variabel 2: $v2<br />
	<br />
	<br />
	<h1>LOOP TESTS</h1><br />
	Loop test:<br />
	<loop name="testloop">$loopvar<br /></loop name="testloop">
	<br />
	empty loop test: <loop name="testloop2">$loopvar<empty name="testloop2">No loops</loop name="testloop2">
	
	<h1>IF TESTS</h1><br />
	if test (True):<br />
	<if name="iftest1">IT IS ALIVE!!!!<else name="iftest1">IT IS NOT ALIVE!!!</if name="iftest1"><br />
	<br />
	if test (False):<br />
	<if name="iftest2">IT IS ALIVE!!!!<else name="iftest2">IT IS NOT ALIVE!!!</if name="iftest2"><br />
<br />
	<h1>Validator Test</h1><br />
<br />
	<!-- <form method="POST" action="index.php"> -->
	<input type="text" name="test1" value="do NOT write 2 here" /><validator type='value' target='test1' compare="2" submitname='submit1' text='O lordy whey did you write "2"???' style='color: red;' /><br />
	<input type="text" name="test2" value="you MUST write something here" /><validator type='empty' target='test2' submitname='submit1' text='Please kind sir.. type something in that field ' style='color: red;' />
	<br />
	Type the same thing in thise to fields!
	<input type="text" name="test3" value="" />
	<input type="text" name="test4" value="" />
	<validator type='same' target='test3' compare='test4' submitname='submit1' text='Why dont you listen to me?' style='color: red;' />
	
	Do NOT select "1"
	<select name="selecttest">
	<option value="1">1</option>
	<option value="2">2</option>
	</select>
	<validator type='value' select="true" target='selecttest' compare="1" submitname='submit1' text='Why do i even try??' style='color: red;' /><br />
	

	<input name="submit1" type="submit" value="Send" />
	<!-- </form> -->



</body>
</html>

My class file:

Code: Select all

<?php
/**
 	FAST TEMPLATE 
 	BY KASPER RUNE SØGAARD
	-----------------------------------
	Version 1
	----------------------------------- 
		 
	***********************************
	Version History
	-----------------------------------
 	Version 1 (27 Okt. 2007 03:59)
 	  * added global vars
 	  * added var setting	
	  * total reworke of vars.
	  * total reworke of loops
	  * total reworke of ifs
	Version 0.41
        * Added loop counut {LOOP.NR}
 	Version 0.4
		* loop within loop, in loop, in
		loop
		* Clear template	
	Version 0.3
		* Added support for if boxes	
	Version 0.2
		* Added support for loops	
	Version 0.1
		* Core was written
	 
	***********************************
	VARS SETUP, replace $var with your var name.
	-----------------------------------	
	$var.ucfirst() >> Upper casced the first letter
	$var.lower() >> ALL letters lower casced
	$var.upper() >> ALL letters upper casced
	$var.ucwords() >> The first letter of each world will be upper casced, the rest will be lower
	
	Numbers only:
	$var.round() >> Rounds the number to the closets full number
	$var.ceil()  >> rounds the number up
	$var.floor()  >> rounds the number down
	$var.day()  >> returnes the day of the month
	$var.month()  >> returnes the moths of the year
	$var.year()  >> returnes the year
	$var.hour() >> returnes the hour of the day
	$var.min() >> returnes the minut of the hour
	$var.sek() >> returnes seks
	
	***********************************
	GLOBAL VARS, the var setup workes here to.
	-----------------------------------
	$global.time = returns the time now
 * 
**/
define("JAVA_PATH", "system/WebResource.axd"); // Stolen from the asp.net MUHAHAHAHAHAHA


if (!class_exists('FastTemplate')) {
	class FastTemplate {
		
		// Setting up vars
		var $root = ""; // This variable contains the path to the template root dir.
		var $files = array(); // This array contains the contents of our templates
		var $loop = array(); // This array contains the doon loops ready to be merged.
		
		// For the validator
		var $req_java = FALSE;
		var $java = ""; // Contrains the java that needs to be put in the head.
		
		function FastTemplate ( $root ) 
		{
			if( !is_dir( $root ) )
			{
				$this->error("Sorry but where not able to find the root dir (" .$root. ")");
			}
			
			$this->root = $root;
		}
		
		function load ( $template )
		{
			if( !is_array( $template ) )
			{
				$this->error("The template need to be an array");
			}
			$keys = array_keys( $template );
			for( $i=0 ; $i <= count($template)-1; $i++ )
			{
				if( !file_exists( $this->root . $template [$keys [$i]] ) )
				{
					$this->error("Sorry but where not able to find the template (" .$this->root . $template [$keys [$i]]. ")");
				}
				$this->files [$keys [$i]] = implode('', file( $this->root . $template [$keys [$i]] ));
			}
		}

		// This var is used to paste var within the class
		function pasteVar($varName, $varValue, $file)
		{		
			$file = str_ireplace('$'.$varName.".ucfirst()",	ucfirst($varValue), $file);
			$file = str_ireplace('$'.$varName.".lower()", strtolower($varValue), $file);
			$file = str_ireplace('$'.$varName.".upper()", strtoupper($varValue), $file);
			$file = str_ireplace('$'.$varName.".ucwords()", ucwords($varValue), $file);
			if(is_numeric($varValue))
			{
				$file = str_ireplace('$'.$varName.".ceil()", ceil($varValue), $file);
				$file = str_ireplace('$'.$varName.".floor()",floor($varValue), $file);
				$file = str_ireplace('$'.$varName.".round()",round($varValue, 0), $file);				
				$file = str_ireplace('$'.$varName.".day()", date("d", $varValue), $file);
				$file = str_ireplace('$'.$varName.".month()",date("m", $varValue), $file);
				$file = str_ireplace('$'.$varName.".year()", date("y", $varValue), $file);
				$file = str_ireplace('$'.$varName.".hour()", date("H", $varValue), $file);
				$file = str_ireplace('$'.$varName.".min()", date("i", $varValue), $file);
				$file = str_ireplace('$'.$varName.".sek()", date("s", $varValue), $file);
			}
			$file = str_ireplace('$'.$varName, $varValue, $file);
			
			return $file;
		}
		
		// Display content if true or false
		function tif($template, $macro, $value)
		{	
			//laver lige lidt syntax clean up
			$this->files [$template] = str_ireplace("<if", "<if", $this->files [$template]);
			$this->files [$template] = str_ireplace("</if", "</if", $this->files [$template]);
			$this->files [$template] = str_ireplace("name=", "name=", $this->files [$template]);
			$this->files [$template] = str_ireplace("<else", "<else", $this->files [$template]);
			
			$true_content = "";
			$fale_content = "";
			$content = "";
			$tmp = explode("<if name=\"".$macro."\">", $this->files [$template]);
			if(count($tmp)>0)
			{
				$content =  explode("</if name=\"".$macro."\">", $tmp[1]);
				// Creating an string with the text that we shale replace
				$replacer = "<if name=\"".$macro."\">".$content[0]."</if name=\"".$macro."\">"; 
				// now we got the content of the if, lets see if there are an else.
				$spicer = explode("<else name=\"".$macro."\">", $content[0]);
				if(count($spicer)>1)
				{
					if($value == TRUE)
					{
						// Replaces with the true content
						$this->files [$template] = str_ireplace($replacer, $spicer[0], $this->files [$template]);	
					}
					else
					{
						// Replacing with the false content
						$this->files [$template] = str_ireplace($replacer, $spicer[1], $this->files [$template]);	
					}
				}
				else
				{
					if($value == TRUE)
					{
						// Replaces with the true content
						$this->files [$template] = str_ireplace($replacer, $content[0], $this->files [$template]);	
					}
					else
					{
						// We have no false output
						$this->files [$template] = str_ireplace($replacer, "", $this->files [$template]);	
					}	
				}
			}		
			return; // Returner erstattede source
		}
		
		// Pasting vars
		function paste ( $template, $array = array() )
		{
			if ( !$this->files [$template] )
			{
				$this->error("Where unable to find the template (".$template.") in the parser", 1);
			}
			if ( !is_array($array) )
			{
				$this->error("There is nothing to parse", 1);
			}
			
			$keys = array_keys($array);
			for ( $i = 0 ; $i <= count($array)-1 ; $i++ )
			{
				$this->files [$template] = $this->pasteVar($keys[ $i ], $array [$keys [$i]], $this->files [$template]);
			}
		}		
		// LOOP FUNCTIONS
		
		// this enables the loop
		function setloop ( $template, $macro )
		{
			//laver lige lidt syntax clean up
			$this->files [$template] = str_ireplace("<loop", "<loop", $this->files [$template]);
			$this->files [$template] = str_ireplace("</loop", "</loop", $this->files [$template]);
			$this->files [$template] = str_ireplace("name=", "name=", $this->files [$template]);
			$this->files [$template] = str_ireplace("<empty", "<empty", $this->files [$template]);
			
			// Getting loop content out of the template
			$content = explode("<loop name=\"" . $macro . "\">", $this->files [$template]);
			$content = explode("</loop name=\"" . $macro . "\">", $content[1]);
			$content = $content[0];
			// Shale we tjek if there was anything?
			if ( $content == "" )
			{
				$this->error ("The loop was empty; where trying to find loop $macro in $template");
				
			}
			
			// Lets get the empty value	
			$tmp = explode("<empty name=\"" . $macro . "\">", $content);
			if(count($tmp)>1)
			{
				$this->loop ["loop." . $macro]['value'] = $tmp[0];
				$this->loop ["loop." . $macro]['empty'] = $tmp[1];	
			}
			else
			{
				$this->loop ["loop." . $macro]['value'] = $content;
				$this->loop ["loop." . $macro]['empty'] = "";
			}
			$this->files ["loop." . $macro] = "";		
			
			// Adding a macro to the original template so we can see where to inset the loopparse
			$replace = "<loop name=\"" . $macro . "\">" . $content . "</loop name=\"" . $macro . "\">";
			$replaceto = "{MACRO: " . $macro . "}";		
			$this->files [$template] = str_replace($replace, $replaceto, $this->files [$template]);
		}	

		function looppaste ( $template, $array )
		{
			if( !$this->loop [$template]['value'] && !$this->loop [$template]['empty'] )
			{
				$this->error("No loop by the name (" .$template. ")", 1);
			}
	
			for ( $i=0; $i <= count($array)-1; $i++ )
			{
				$keys = array_keys($array [$i]);
				$tmp = $this->loop [$template]['value'];
	        
	            $tmp = str_replace('$'."global.loopnr()", $i, $tmp);  
				
				for ( $u=0; $u <= count($keys)-1 ; $u++ )
				{
					$tmp = $this->pasteVar($keys[ $u ], $array [$i] [$keys [$u]], $tmp);
				}
	
				$this->files [$template] .= $tmp;
			}
		}				
		function macromerge ( $template, $macro )
		{
			if($this->files ["loop." . $macro] != "")
			{
				$return = $this->files ["loop." . $macro];
			}
			else
			{
				$return = $this->loop ["loop." . $macro]['empty'];
			}
			
			$this->files [$template] = str_replace("{MACRO: " . $macro . "}", $return, $this->files [$template]);
		}
		// Done with the loop function
			
		// This function can merge to templates together	
		function merge ( $template, $target, $where )
		{
			if( (!(isset($this->files [$template]))) || (empty($this->files [$template])) )
			{
				$this->error("No template by the name (" . $template . ") was found");
			}
			if( (!(isset($this->files [$target]))) || (empty($this->files [$target])) )
			{
				$this->error("No template by the name (" . $target . ") was found");
			}		
			if( substr_count($this->files [$target], $where) < 1 )
			{
				$this->error("The string (" . $where . ") was not found in the template (" . $target . ")");
			}		
			
			
			if( !$this->files [$target] = str_replace($where, $this->files [$template], $this->files [$target] ) )
			{
				$this->error("Where unable to merge the 2 templates");
			}
		}			
		
		// This will clear out the template, to save ram, when you don't need it any more	
		function clear ( $template )
		{
			if( isset($this->files [$template]) )
			{
				unset( $this->files [ $template ] );
			}	
		}	
			
		// Returns the template ready to be echoed
		function techo($template, $validate = false)
		{
			if( (!(isset($this->files [$template]))) || (empty($this->files [$template])) )
			{
				$this->error("Nothing to parsed, nothing to printed");
				return;
			}
			else
			{
				// Lets do some html clean up
				$this->files [$template] = str_replace("Å", "&Aring;", $this->files [$template]);
				$this->files [$template] = str_replace("Ø", "&Oslash;", $this->files [$template]);
				$this->files [$template] = str_replace("Æ", "&AElig;", $this->files [$template]);
				$this->files [$template] = str_replace("å", "&aring;", $this->files [$template]);
				$this->files [$template] = str_replace("ø", "&oslash;", $this->files [$template]);
				$this->files [$template] = str_replace("æ", "&aelig;", $this->files [$template]);
				
				// Globals
				$this->files [$template] = $this->pasteVar("global.time", time(), $this->files [$template]);
				
				if($validate) $this->validator($template);
				
				if($this->req_java)
				{
					$this->files [$template] = str_ireplace("</head>", "<script type=\"text/javascript\">\n".$this->java."</script>\n</head>", $this->files [$template]);	
				}
				
				echo $this->files [$template];
				return( $this->files [$template] );
			}	
		}
		
		private function error($error)
		{
			trigger_error($error, E_USER_ERROR);
		}
		
		private function validator($template)
		{
			$tp = &$this->files [$template];
			$submit = array(); // If there are more then 1 validator, then will this help with the ekstra number of functions
			// Ok lets start out with finding oure validators in oure template
			// This is just so we don't have a chace error
			$tp = str_ireplace("<validator", "<validator", $tp);

			$validators = array();
			$tmp = explode("<validator", $tp);
			$loop_nr = 0;
			for($i=1;$i<=count($tmp)-1;$i++)
			{
				$content = explode(">", $tmp[$i]);
				$validators[$loop_nr]['full'] = "<validator".$content[0].">";
				unset($t);
				preg_match("/.*type=\'(.*?)\'.*$/", $content[0], $t);
				if($t[1] == "") preg_match("/.*type=\"(.*?)\".*$/", $content[0], $t);
				$validators[$loop_nr]['type'] = $t[1];
				
				unset($t);
				preg_match("/.*select=\'(.*?)\'.*$/", $content[0], $t);
				if($t[1] == "") preg_match('/.*select="(.*?)".*$/', $content[0], $t);
				$validators[$loop_nr]['select'] = $t[1];
				
				unset($t);
				preg_match("/.*text=\'(.*?)\'.*$/", $content[0], $t);
				if($t[1] == "") preg_match('/.*text="(.*?)".*$/', $content[0], $t);
				$validators[$loop_nr]['text'] = $t[1];
				if($validators[$loop_nr]['text'] == "") $validators[$loop_nr]['text'] = "*";
				
				unset($t);
				preg_match("/.*target=\'(.*?)\'.*$/", $content[0], $t);
				if($t[1] == "") preg_match('/.*target="(.*?)".*$/', $content[0], $t);
				$validators[$loop_nr]['target'] = $t[1];
				
				unset($t);
				preg_match("/.*compare=\'(.*?)\'.*$/", $content[0], $t);
				if($t[1] == "") preg_match('/.*compare="(.*?)".*$/', $content[0], $t);
				$validators[$loop_nr]['compare'] = $t[1];
				
				unset($t);
				preg_match("/.*style=\'(.*?)\'.*$/", $content[0], $t);
				if($t[1] == "") preg_match('/.*style="(.*?)".*$/', $content[0], $t);
				$validators[$loop_nr]['style'] = $t[1];
				
				unset($t);
				preg_match("/.*submitname=\'(.*?)\'.*$/", $content[0], $t);
				if($t[1] == "") preg_match('/.*submitname="(.*?)".*$/', $content[0], $t);
				$validators[$loop_nr]['submitname'] = $t[1];
				
				$validators[$loop_nr]['returntext'] = "<span style=\"" . $validators[$loop_nr]['style'] . ">" . $validators[$loop_nr]['text'] . "</span>";
				$validators[$loop_nr]['id'] = "validator" . $loop_nr;
				
				
					// Adding the javascript function to the inputs
					$tp = str_ireplace("name=\"" . $validators[$loop_nr]['target'] . "\"", "name=\"" . $validators[$loop_nr]['target'] . "\" onchange=\"" . $validators[$loop_nr]['id'] . "()\"", $tp);
					
					$submit[$validators[$loop_nr]['submitname']] .= $validators[$loop_nr]['id'] . "();";

					$tp = str_ireplace($validators[$loop_nr]['full'], "<div id=\"" . $validators[$loop_nr]['id'] . "\"></div>", $tp);

				if($validators[$loop_nr]['type'] == "same")
				{ 
					$tp = str_ireplace("name=\"" . $validators[$loop_nr]['compare'] . "\"", "name=\"" . $validators[$loop_nr]['compare'] . "\" onchange=\"" . $validators[$loop_nr]['id'] . "()\"", $tp);
					
					 $this->java .= "function ".$validators[$loop_nr]['id']."() {
 var field1 = document.getElementById('".$validators[$loop_nr]['target']."');
 var field2 = document.getElementById('".$validators[$loop_nr]['compare']."');

 var answer = document.getElementById('".$validators[$loop_nr]['id']."');

 if (field1.value == field2.value) { 
   answer.innerHTML=\"\"; 
 } else { 
   answer.innerHTML=\"<span style=\'" . $validators[$loop_nr]['style'] . "\'>".$validators[$loop_nr]['text']."</span>\"; 
 }; 
};

";
				}
				elseif($validators[$loop_nr]['type'] == "empty")
				{
					 $this->java .= "function ".$validators[$loop_nr]['id']."() {
 var field1 = document.getElementById('".$validators[$loop_nr]['target']."');

 var answer = document.getElementById('".$validators[$loop_nr]['id']."');

 if (field1.";
 if($validators[$loop_nr]['select'] != "") $this->java .= "options[field1.selectedIndex].";
 $this->java .= "value == \"\") { 
   answer.innerHTML=\"<span style=\'" . $validators[$loop_nr]['style'] . "\'>".$validators[$loop_nr]['text']."</span>\"; 
 } else { 
   answer.innerHTML=\"\"; 
 }; 
};

";
				}elseif($validators[$loop_nr]['type'] == "value")
				{
					 $this->java .= "function ".$validators[$loop_nr]['id']."() {
 var field1 = document.getElementById('".$validators[$loop_nr]['target']."');

 var answer = document.getElementById('".$validators[$loop_nr]['id']."');

 if (field1.";
 if($validators[$loop_nr]['select'] != "") $this->java .= "options[field1.selectedIndex].";
 $this->java .= "value == '".$validators[$loop_nr]['compare']."') { 
   answer.innerHTML=\"<span style=\'" . $validators[$loop_nr]['style'] . "\'>".$validators[$loop_nr]['text']."</span>\"; 
 } else { 
   answer.innerHTML=\"\"; 
 }; 
};

";
				}
				
				$loop_nr++;
			}
			unset($tmp, $content, $t, $loop_nr);
			
			$keys = array_keys($submit);
			for($i=0;$i<=count($keys)-1;$i++)
			{
				$tp = str_ireplace("name=\"" . $keys[$i] . "\"", "name=\"" . $keys[$i] . "\" onchange=\"" . $submit[$keys[$i]] . "\"", $tp);
			}
			
			if(count($validators)>0) $this->req_java = true;
			
			return true;
		}
	}
}
?>

Thanks for your help
Andromeda
Kasper Rune Søgaard

Posted: Thu Nov 08, 2007 7:47 pm
by Christopher
There are a number of errors in the template (tested in Firefox). The <input>'s need id= in addition to name= properties. And the following string cause a javascript error -- you should escape strings:

Code: Select all

text='O lordy whey did you write \"2\"???'
So what is it? The code is a little crazy. Is is a template class or a forms class?

Posted: Fri Nov 09, 2007 2:20 am
by Andromeda
I woulden't say that the code is crazy, othere then the java scipt, that is only because, i don't know javascript, and i just just handet over a java script that i tryed to make worke.