<?php /********************************************************************************* * 模板類(Template) * 最後修改時間:2004.4.07 本論壇使用 * * * **********************************************************************************/ class Template { //$this->$template,儲存模板資料. var $template = ''; //模板路徑. var $tpl_path = ''; //模板首碼(風格名稱). var $tpl_prefix = ''; //cache路徑(編譯後的路徑). var $cache_path = ''; //css檔案路徑. var $css_path = ''; //header檔案路徑. var $header_path = ''; //footer檔案路徑 var $footer_path = ''; /** * 初始化模板路徑. */ function Template($root = 'default') { //模板首碼(風格名稱). $this->tpl_prefix = $root; //模板檔案路徑. $this->tpl_path = './templates/' . $root . '/'; //產生的PHP檔案存放路徑. $this->cache_path = './template_data/' .$this->tpl_prefix . '_'; return true; } /** * chk_cache,檢查"編譯"後的模板是否需要更新,判斷依據:最後修改時間,"編譯"檔案是否存在. */ function chk_cache($tpl_index) { $tpl_file = $this->tpl_path . $tpl_index . '.html'; $cache_file = $this->cache_path . $tpl_index . '.php'; //判斷是否需要更新. if(!file_exists($cache_file)) { return true; } elseif(filemtime($tpl_file) > filemtime($cache_file)) { return true; } } /** * 輸出模板檔案. */ function parse_tpl($tpl_index,$message='') { return $this->cache_path . $tpl_index . '.php'; } /** * 載入模板檔案. */ function load_tpl($tpl_index) { $tpl_file = $this->tpl_path . $tpl_index . '.html'; $fp = fopen($tpl_file, 'r'); $this->template = fread($fp, filesize($tpl_file)); fclose($fp); } /** * 替換變數,並且"編譯"模板. */ function write_cache($tpl_index) { $cache_file = $this->cache_path . $tpl_index . '.php'; //變數顯示. $this->template = preg_replace("/(\{=)(.+?)(\})/is", "<?=\\2?>", $this->template); //介面語言替換. $this->template = preg_replace("/\{lang +(.+?)\}/ies", "\$lang['main']['\\1']", $this->template); $fp = fopen($cache_file, 'w'); flock($fp, 3); fwrite($fp, $this->template); fclose($fp); } /** * 替換block. */ function assign_block($search,$replace) { $this->template = str_replace($search,$replace,$this->template); } } ?> |