<?
Class Tpl{
  public  $path         = '';          
  public  $plugins      = '';          
  public  $ext          = '.tpl.php';  
  public  $all_header   = null;      
  public  $all_footer   = null;        
  public  $header       = null;
  public  $footer       = null;
  public  $tpl404       = "404";
  public  $PAGE         = array();
  private $_data        = array();
  private $_templates = array();    
  
	function display($tpl,$full = true){
    $load_tpl = $this->path.$tpl.$this->ext;
    $load_tpl = is_file($load_tpl) ? $load_tpl : $tpl.$this->ext;
		if(is_file($load_tpl)){ 
      if($full){
        
        if($this->all_header && is_file($this->path.$this->all_header.$this->ext)){
          include($this->path.$this->all_header.$this->ext);
        }
        
        if($this->header && is_file($this->path.$this->header.$this->ext)){
          include($this->path.$this->header.$this->ext);
        }
      }  
        
			include($load_tpl);
        
      if($full){
        if($this->footer && is_file($this->path.$this->footer.$this->ext)){
          include($this->path.$this->footer.$this->ext);
        }
        
        if($this->all_footer && is_file($this->path.$this->all_footer.$this->ext)){
          include($this->path.$this->all_footer.$this->ext);
        }
      }

		}else{
			die("Нет шаблона ".$load_tpl);
		}
	}

  public function fetch($tpl,$full=false) {
      ob_start();
      $this->display($tpl,$full);
      $content = ob_get_contents();
      ob_clean();
      return $content;
  }
	
  function error404(){
    header("HTTP/1.0 404 Not Found");
    $this->PAGE = array('meta_title'=>'404 ERROR Нет такой страницы','meta_name'=>'404 ERROR Нет такой страницы');
		$this->header     = "main/header";
	  $this->footer     = "main/footer";
    $this->display($this->tpl404);
    die();
  }
   
  public function __set($key, $value) {
    $this->_data[$key] = $value;
  }
  
  public function &__get($key) {
    return $this->_data[$key];
  }
  
  public function __call($method, $args = array()){
    $funcName = "Tpl_modifier_{$method}"; 
    if(function_exists($funcName)===false){
      if(is_file($this->plugins."modifer.{$method}.php")){
        include($this->plugins."modifer.{$method}.php");
      }
    }
    if(function_exists($funcName)){
      return call_user_func_array($funcName, $args);
    }else{
      return false;
    }
  }
  
}