to ?> 1.0 - Initial release This class requires globals to be switched on in order for this to work. Once on, you can perform the following method to dynamically replace published variables. template::run("Hello %var._SERVER.REMOTE_ADDR|arg0%, nice to meet you."); This will replace the published variable with the PHP value for: $_SERVER["REMOTE_ADDR"] and arg0 is the replacement if the variable doesn't exist. We have also added nest support, so we can have the following. template::run("Hello %file.some/file.php|arg0|arg1%, nice to meet you."); This will load up this file and output the contents, if execFile is true, then PHP will be processed. This also means you can access the arguments through $_variables on the file eg: $_variables = array("arg0", "arg1") is what is passed through with the arguments. template::run("The unix time is: %function.time% and real date is: %function.date|d/m/Y% As you can see, we can run PHP functions through this system now, you can pass through as many arguments as you wish, this will be passed directly into the function called, simple. %function.core::method("time")->now% From the example above, you can see that we can access functions within the framework, this is accessing the core scope and you must use the method() function, inside here, we declare the class we wish to enter and from there, we supply the function we wish to run. template::set("execFile", BOOLEAN); This will allow the files loaded to parse PHP or not. */ class template { var $execFile = true; var $version = "1.2"; function __construct(){ $this->snippet = array(); $this->updated = array(); $this->error = array(); $this->variables= array(); $name = $_SERVER; } //end function __construct(); function set($variable, $value){ $this->$variable = $value; } //end function set(); function getUniqueID(){ //Get a count for both arrays. $first = count($this->snippet); $last = count($this->updated); //Challenge which is highest and return it. $id = ($first >= $last) ? $first : $last; return $id; } //end function getUniqueID(); function run($content){ //Get a unique ID. $id = $this->getUniqueID(); //Add snippet to origional list. $this->snippet[$id] = $content; //Repalce the code and add to updated list. //Pass the ID. Less transfer, faster response times. $this->updated[$id] = $this->sortVariables($id, 3); $this->updated[$id] = $this->sortVariables($id, 2); $this->updated[$id] = $this->sortVariables($id, 1); //Return the output; return $this->updated[$id]; } //end function run(); function validID($id){ return (isset($this->snippet[$id])) ? true : false; } //end function validID(); function error($error=""){ core::method("error")->error($error); } //end function error(); function sortVariables($id, $level="2"){ //Return an error if the ID is not valid. if (!$this->validID($id)) return $this->error("Error, template snippet '$id' does not exist."); //Because we're this far, the ID is valid. $content = $this->snippet[$id]; preg_match_all("/%function\.(.*?)%/", $content, $func, PREG_OFFSET_CAPTURE); foreach ($func[1] as $key => $val){ $part = explode("|", $val[0]); $aarg = $part; unset($aarg[0]); ksort($aarg); ob_start(); echo eval("return ".$part[0]."(\"".implode("\",\"", $aarg)."\");"); $output = ob_get_clean(); $content = str_replace("%function.".$val[0]."%", $output, $content); } //end foreach preg_match_all("/%file\.(.*?)%/", $content, $files, PREG_OFFSET_CAPTURE); foreach ($files[1] as $__key => $__val){ $file = explode("|", $__val[0]); ob_start(); if ($this->execFile == true){ //We need to set the core variable. include("classes/config.php"); $_argv = $_variables = $file; unset($_variables[0]); sort($_variables); $_argc = count($_argv); @include($file[0]); } else { $content = @file_get_contents($file[0]); $content = str_replace("", "?>", $content); echo $content; } //end if $text = ob_get_clean(); $text = $this->run($text); $content = str_replace("%file.".$__val[0]."%", $text, $content); } //end foreach preg_match_all("/%var\.(.*?)%/", $content, $matches, PREG_OFFSET_CAPTURE); preg_match_all("/%var\.(.*?)\|%var.(.*?)%%/", $content, $matches2, PREG_OFFSET_CAPTURE); preg_match_all("/%var\.(.*?)\|%var.(.*?)\|%var.(.*?)%%%/", $content, $matches3, PREG_OFFSET_CAPTURE); $check = ($level != "3") ? ($level != "2") ? $matches["1"] : $matches2["2"] : $matches3["3"]; foreach ($check as $key => $val){ //Split the variable down into the sections. $split = explode("|", $val[0]); $name = explode(".", $split[0]); $options= $split; unset($options[0]); sort($options); //Compile the variable into a readable array. $varList = array( "origional" => "%var.".$val[0]."%", "variable" => $name, "options" => $options, ); $this->variables[] = $varList; } //end foreach $output = $this->replaceVariables($content); $this->snippet[$id] = $output; return $output; } //end function sortVariables(); function replaceVariables($content){ foreach ($this->variables as $key => $val){ $count = 0; $name = ""; $value = ""; $failed = false; $failSafe = implode(".", $val["variable"]); if (isset($val["options"][0])) $failSafe = $val["options"][0]; //print_r($val); foreach($val["variable"] as $a => $b){ if ($count == "0"){ if (isset($GLOBALS[$b])){ $value = $GLOBALS[$b]; } else { $value = $failSafe; $failed = true; } //end if } //end if if ($count >= "1" AND $failed != true){ $value = (isset($value[$b])) ? $value[$b] : $val["options"]["0"]; //endsure a variable is not undeclaired. } //end if $count++; } //end foreach //Replace the content. $content = str_replace($val["origional"], $value, $content); } //end foreach(); //Return the content. return $content; } //end function replaceVariables(); } //end class template(); ?>