Location: PHPKode > scripts > Template Manager > template-manager/template.php
<?php 
class template{ 
    var $path, $file, $content, $name, $conf; 
    var $vars = array(); 
     
    function template(){ 
        switch( func_num_args() ) 
        { 
            case 2: 
                $this->con2($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(){ 
        preg_match_all("/{include (.*?)}/", $this->content, $matches, PREG_SET_ORDER); 
        foreach($matches as $key => $val){ 
            $fh = fopen($this->path.$val[1].'.tpl', 'r'); 
            $buf = ""; 
            while(!feof($fh)){ 
                $buf .= fread($fh, 128); 
            } 
            str_replace("{*".$val[1]."*}", $buf, $this->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;
		$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();
        $fh = fopen($this->path.$this->file, 'r');
        $buf = "";  
        while(!feof($fh)){  
            $buf .= fread($fh, 128);  
        }   
        $this->content = $buf;  
        $this->prepFile();  
        $this->prepRender($this->vars);  
        return $this->content;  
    }
} 
?>
Return current item: Template Manager