**文件:templates.class.php**
屬于解析式模板類,通過獲得標簽位置進行內容替換,數組$rules用于存儲模板標簽的解析規則。
#### * 常用方法:
1、private function _parse($string) 解析標簽;
2、private function _compile() 生成緩存,第二次不需要解析模板;
3、public function view() 將模板內容在頁面中顯示。
#### * 使用步驟:
**1、載入模板:**
~~~
require $this->tpl('index');
~~~
**2、初始化:**
~~~
$ins = new template($tplName,$path,$cachepath);
//傳入文件名稱,文件初始加載路徑,文件緩存路徑。
~~~
**3、解析標簽**
~~~
foreach($this->rules as $key => $value){
//使用正則匹配標簽及其對應的解析規則,并且進行替換
if(is_array($value)){
$string = preg_replace_callback($key, create_function(
'$data', '$data = '.$value[1].'>=0?$data['.$value[1].']:$data;
return tag::'.$value[0].'( $data );'), $string);
}else{
$string = preg_replace($key, $value, $string);
}
}
~~~
**4、顯示或保存為HTML**
~~~
return $ins->view();
~~~