<?php
class template{
var $path, $file, $content, $name, $conf;
var $vars = array();
function template(){
switch( func_num_args() )
{
case 2:
$this->con1($argv[0], $argv[1]);
break;
case 3:
$this->con2($argv[0], $argv[1], $argv[2]);
break;
case 4:
$this->con3($argv[0], $argv[1], $argv[2]);
break;
}
}
public function setFile($file){
$this->file = $file.'.tpl';
}
private function con1($name, $file){
$this->file = $file.'.tpl';
$this->path = "templates/$name/";
}
private function con2($name, $file, $content){
$this->file = $file.'.tpl';
$this->path = "templates/$name/";
$this->vars = $content;
}
private function con3($name, $file, $content){
$this->file = $file.'.tpl';
$this->path = "templates/$name/";
$this->vars = $content;
return $this->Render();
}
public function setName($name){
$this->path = "templates/$name/";
$this->name = $name;
}
public function passVars($var){
foreach($var as $key => $val){
$this->vars[$key] = $val;
}
}
public function passVar($name, $val){
array_merge($this->vars, array("$name" => $val));
}
private function prepFile(){
$this->prepCount();
$this->prepForeach();
$this->prepFor();
$this->prepInclude();
$this->prepIf();
}
private function prepIf(){
preg_match_all("/{if (.*[^\t]) (.*[^\t]) (.*[^,]), (.*[^,]), (.*[^}])}/", $this->content, $matches, PREG_SET_ORDER);
foreach($matches as $key => $val){
$replace = "";
switch($val[2]){
case "==":
if($val[1] == $val[3]){$replace = $val[4];}else{$replace = $val[5];};
break;
case "!=":
if($val[1] != $val[3]){$replace = $val[4];}else{$replace = $val[5];};
break;
case "<":
if($val[1] < $val[3]){$replace = $val[4];}else{$replace = $val[5];};
break;
case "<=":
if($val[1] <= $val[3]){$replace = $val[4];}else{$replace = $val[5];};
break;
case ">":
if($val[1] > $val[3]){$replace = $val[4];}else{$replace = $val[5];};
break;
case ">=":
if($val[1] >= $val[3]){$replace = $val[4];}else{$replace = $val[5];};
break;
}
$replace = $this->functionRender($this->vars, $replace);
$this->content = str_replace($val[0], $replace, $this->content);
}
}
private function prepForeach(){
preg_match_all("/{foreach (.*[^,]), (.*[^}])}/" , $this->content, $matches, PREG_SET_ORDER);
foreach($matches as $key => $val){
$text = split('.', $val[1]);
foreach($text as $nkey => $nval){
if(!isset($array)){
$array = $this->vars[$nval];
}else{
$array = $array[$nval];
}
}
$var = $val[1];
if(!isset($array)) $array = $this->vars[$var];
$final = '';
foreach($array as $nkey => $nval){
$temp = $val[2];
$final .= str_replace("[i]", $val[1].'.'.$nkey, $temp);
}
$final = $this->functionRender($this->vars, $final);
$this->content = str_replace($val[0], $final, $this->content);
}
}
private function prepInclude(){
preg_match_all("/{include (.*[^}])}/", $this->content, $matches, PREG_SET_ORDER);
foreach($matches as $key => $val){
ob_start();
include($this->oath.$val[1].'.tpl');
$buf = ob_get_clean();
str_replace("{include ".$val[1]."}", $buf, $this->content);
}
}
private function prepCount(){
preg_match_all("/{(.*[^count]) count\((.*[^\)])\) (.*[^}])}/", $this->content, $matches, PREG_SET_ORDER);
foreach($matches as $key => $val){
$text = split('.', $val[2]);
foreach($text as $nkey => $nval){
if(!isset($array)){
$array = $this->vars[$nval];
}else{
$array = $array[$nval];
}
}
$out = count($array);
$this->content = str_replace($val[0], "{".$val[1].$out.$val[3]."}", $this->content);
}
}
private function prepFor(){
preg_match_all("/{for (.*[^;]); (.*[^\t]) (.*[^\t]) (.*[^;]); (.*[^,]), (.*[^}])}/", $this->content, $matches, PREG_SET_ORDER);
foreach($matches as $key => $val){
if($val[1][0] != '$'){$old = $val[1][0]; $val[1] = '$'.$val[1];}
if($val[2][0] != '$')$val[2] = '$'.$val[2];
if($val[5][0] != '$')$val[5] = '$'.$val[5];
eval('for('.$val[1].'; '.$val[2].' '.$val[3].' '.$val[4].'; '.$val[5].'){
$data = "'.$val[6].'";
$data = str_replace("[$old]","$i", $data);
$out .= $data;
}');
$out = $this->functionRender($this->vars, $out);
$this->content = str_replace($val[0],$out,$this->content);
}
}
private function functionRender($vars, $content, $head = null){
if(is_null($head)){
foreach($vars as $key => $val){
if(is_array($val)){
$content = $this->functionRender($val, $content, "$key");
}else{
$find = "%$key%";
$content = str_replace($find, $val, $content);
}
}
}else{
foreach($vars as $key => $val){
if(is_array($val)){
$content = $this->functionRender($val, $content, "$head.$key");
}else{
$find = "%$head.$key%";
$content = str_replace($find, $val, $content);
}
}
}
return $content;
}
private function prepRender($content, $head = null){
if(is_null($head)){
foreach($content as $key => $val){
if(is_array($val)){
$this->prepRender($val, "$key");
}else{
$find = "{%$key%}";
$this->content = str_replace($find, $val, $this->content);
}
}
}else{
foreach($content as $key => $val){
if(is_array($val)){
$this->prepRender($val, "$head.$key");
}else{
$find = "{%$head.$key%}";
$this->content = str_replace($find, $val, $this->content);
}
}
}
}
private function parseTemplate(){
$name = $this->name;
if($fh = @fopen($this->path.$name.".conf", 'r')){
$buf = "";
$out = array();
while(!@feof($fh)){
$buf .= @fread($fh, 128);
}
$lines = @explode("\n", $buf);
foreach($lines as $key => $val){
$chars = str_split($val);
$slashCheck = $chars[0].$chars[1];
if($chars[0] != '#' && $slashCheck != '//' && $chars[0] != ''){
$data = explode(' = ', $val);
$nKey = $data[0];
$nVal = $data[1];
$out[$nKey] = $nVal;
}
}
return $out;
}
}
public function render(){
if(!isset($this->file)) $this->file = "index.tpl";
if(!isset($this->path)) $this->setName('default');
if(!isset($this->vars)) $this->vars = $GLOBALS;
$this->vars['Template'] = $this->parseTemplate();
ob_start();
include($this->path.$this->file);
$buf .= ob_get_clean();
$this->content = $buf;
$this->prepFile();
$this->prepRender($this->vars);
return $this->content;
}
}
?>