一個簡單的模板類(PHP)

接上話,有了一個數據操作類,這樣項目只能簡單地操作數據,但是要達到能夠和美工一起顯示美好的頁面,則需要一個比較好的模板引擎。與SMARTY這樣比較龐大的模板引擎相比,我想下面這個則真算得小得多了。

這個模板類是我以前在網上看到的,寫得比較好,所以我就引用了,還不知道作者是誰,這裏就先講一下這個類的原理。

首先,這個類只有一個簡單的正則解析器。但是基本上可以用到了。如果再能夠在此基礎上擴展,我相相信這個小東西很有發展,請有相同愛好的同志們,來加入到強化他的目地。我這就拋下磚。

template.class.php

 

<?php
class template{
	
	//變量儲存的數組
	private $vars = array();
	//模板目錄
	public $template_dir = './template/';
	//緩存目錄 
	public $cache_dir = './cache/';
	//編譯目錄
	public $template_c_dir = './template_c/';
	//模板文件
	public $template_file = '';
	//左連接符
	public $left_delimiter = '<{';
	//右連接符
	public $right_delimiter = '}>';
	//編譯文件
	private $template_c_file = '';
	//緩存文件
	private $cache_file = '';
	//緩存時間
	public $cache_time = 0;
	
	//內置解析器
	private $preg_temp = array(
		'~<\{(\$[a-z0-9_]+)\}>~i'
            => '<?php echo $1; ?>', // <{$name}>
        
        '~<\{(\$[a-z0-9_]+)\.([a-z0-9_]+)\}>~i'
            => '<?php echo $1[\'$2\']; ?>', // <{$arr.key}>
        
        '~<\{(\$[a-z0-9_]+)\.([a-z0-9_]+)\.([a-z0-9_]+)\}>~i'
            => '<?php echo $1[\'$2\'][\'$3\']; ?>', // <{$arr.key.key2}>
        
        '~<\?php\s+(include_once|require_once|include|require)\s*\(\s*(.+?)\s*\)\s*;?\s*\?>~i'
            => '<?php include \$this->_include($2); ?>', // <?php include('inc/top.php'); ?>
        
        '~<\{:(.+?)\}>~' => '<?php echo $1; ?>', // <{:strip_tags($a)}>
        
        '~<\{\~(.+?)\}>~' => '<?php $1; ?>', // <{~var_dump($a)}>
        
        '~<\?=\s*~' => '<?php echo ', // <?=
	);
	
	/**
	 *構造器
	 */
	public function __construct(){
		if(defined('TMP_PATH')){
			$this->template_c_dir = TMP_PATH . 'template_c/';
			$this->cache_dir	  = TMP_PATH . 'cache/';
		}
	}
	
	/**
	 *變量賦值
	 *@param $key mixed 鍵名
	 *@param $value mixed 值
	 */
	public function assign($key , $value = ''){
		if(is_array($key)){
			$this->vars=array_merge($key,$this->vars);
		}
		else{
			$this->vars[$key]=$value;
		}
	}
	
	/**
	 *顯示頁面
	 *@param $file string 模板文件名
	 */
	public function display($file){
		echo $this->fetch($file);
	}
	
	/**
	 *返回緩存區內容
	 *@param $file string 模板文件名
	 *@return $content string 緩存內容
	 */
	public function fetch($file){
		$this->template_file = $file;
		$desc_template_file = $this->template_dir .$file;
		$desc_content = $this->readfile($desc_template_file);
		
		$template_c_file_time= filemtime($desc_template_file);
		//若在超過緩存時間,則編譯
		if($this->cache_time < time()-$template_c_file_time){
			$this->complie($this->token($desc_content));
		}
		//以下獲取緩存區的內容
		ob_start();
		
		@extract($this->vars , EXTR_OVERWRITE);
		include ($this->template_c_dir . $this->template_c_file);
		
		$content = ob_get_contents();
		ob_end_clean();
		
		//$this->store_buff($content);
		return $content;
	}
	
	/*
	 *替換分隔符,以及替換解析器的內容
	 *@param $content string 讀取的內容
	 *@return $token_content string 替換後的內容
	 */
	public function token($content){
		$token_content = $content;
		if($left_delimiter != '<{'){
			$token_content = str_replace($left_delimiter , '<{' , $token_content);
			$token_content = str_replace($right_delimiter, '}>' , $token_content);
		}
		$token_content = preg_replace(array_keys($this->preg_temp), $this->preg_temp , $token_content);
		return $token_content;
	}
	
	/*
	 *生成儲存
	 *@param $content string 讀取的內容
	 *
	 
	public function store_buff($content){
		$this->cache_file = md5($this->template_file) . $this->template_file . '.html';
		$tempfile = $this->cache_dir . $this->cache_file;
		$fp = fopen($tempfile , 'w');
		fputs($fp,$content);
		fclose($fp);
		unset($fp);
	}
	*/
	
	/*
	 *編譯儲存
	 *@param $content string 讀取的內容
	 *
	 */
	public function complie($content){
		$this->template_c_file = md5($this->template_file) . $this->template_file . '.php';
		$tempfile = $this->template_c_dir . $this->template_c_file;
		$fp = fopen($tempfile , 'w');
		fputs($fp,$content);
		fclose($fp);
		unset($fp);
	}
	
	/*
	 *讀取文件的內容
	 *@param $file string 文件名
	 *@return $content string 文件內容
	 */
	public function readfile($file){
		if(file_exists($file)){
			$fp = fopen($file , 'r');
			$content ='';
			while(!feof($fp)){
				$content .= fgets($fp,4096);
			}
			fclose($fp);
			unset($fp);
			return $content;
		}
		else{
			exit($file . ' not exist!');
		}
	}
	
	/*
	 *模板嵌套
	 *@param $file string 文件名
	 *@return string 文件的絕對地址
	 */
	public function _include($file){
		if(file_exists($this->template_dir . $file)){
			return ($this->template_dir . $file);
		}
		else{
			echo "模板文件不存在";
			exit;
		}
	}
}
?>

有了這個模板,你可以忘記那些大型的模板,畢竟你的美工不會在他的切片文件裏寫那麼多SMARTY的標記,而只要他切好,你可以直接用,不用去修改images , css ,js 地址了。
發佈了48 篇原創文章 · 獲贊 16 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章