代码: |
require('./template.php'); //由html生成的php文件的前缀,区别使用多种风格. $tpl_prefix = 'default'; //模板文件名 $tpl_index = 'index'; $tpl = new Template($tpl_prefix); $cats = array( array('forum_id'=>'1','forum_cat_id'=>'0','forum_name'=>'PHP学习'), array('forum_id'=>'2','forum_cat_id'=>'0','forum_name'=>'MYSQL学习') ); $forums = array( array('forum_id'=>'3','forum_cat_id'=>'1','forum_name'=>'PHP高级教程'), array('forum_id'=>'4','forum_cat_id'=>'1','forum_name'=>'PHP初级教程'), array('forum_id'=>'5','forum_cat_id'=>'2','forum_name'=>'MYSQL相关资料') ); if ($cats) { if ($tpl->chk_cache($tpl_index))//检查判断是否需要重新生产PHP模板文件. { $tpl->load_tpl($tpl_index);//加载html模板文件. //替换PHP语句 $tpl->assign_block("{block_cat}",""); $tpl->assign_block("{/block_cat}","}?>"); $tpl->assign_block("{block_forum}","\nif(\$forum['forum_cat_id'] == \$cat['forum_id']) {?>"); $tpl->assign_block("{/block_forum}","}\n}?>"); //生产PHP模板文件. $tpl->write_cache($tpl_index); } } //包含PHP模板文件. include($tpl->parse_tpl($tpl_index)); ?> |
代码: | |||||||
{block_cat}
{/block_cat} |
代码: | |||||||
}?> |
代碼: |
/**************************************************** ***************************** * * 最後修改時間:2004.4.07 本論壇使用 * * * ********************************* *************************************************/ class Template { //$this->$template,儲存範本資料. var $template = ''; ///範本路徑. var $tpl_path = ''; //範本前綴(樣式名稱). var $tpl_prefix = ''; //cacheache(編譯後的路徑). var $cache_path = ''; //css檔案路徑. var $css_path = ''; //header檔案路徑. ''; //footer檔案路徑 var $footer_path = ''; /** * 初始化範本路徑. */ function Template($root = 'default (樣式名稱). $this->tpl_prefix = $root; //範本檔案路徑. $this->tpl_path = './templates/ $this->d_path = './templates/ $ 🎜> //產生的PHP檔案存放路徑. $this->cache_path = './template_data/' .$this->tpl_prefix . '_'; /** * chk_cache,檢查"編譯"後的範本是否需要更新,判斷依據:最後修改時間,"編譯"檔案是否存在. */ function chk_cache($tpl_index) { $ >cache_path . $tpl_index . '.php'; //判斷是否需要更新. if(!file_exists($cache_file)) } elseif(filemtime($tpl_file) > filemtime($cache_file)) { return true; return true; function parse_tpl($ tpl_index,$message='') { return $this->cache_path . $tpl_index . '.php'; } . ($tpl_index) { $tpl_file = $this->tpl_path . $tpl_index . '.html'; 1$fp = ffile$t_p. >template = fread($fp, filesize($tpl_file)); fclose($fp); } /** * 輸出範本檔. */ { $cache_file = $this->cache_path . $tpl_index . '.php'; //變數顯示。 ?)(})/is", "=\2?>", $this->template); //介面語言替換. $this->template = preglang_replace("/{ +(.+?)}/ies", "$lang['main']['\1']", $this->template); $fp = fopen($cache_file, 'w'); flock($fp, 3); fwrite($fp, $this->template); fclose($fp); function assign_block($search,$replace) { $this->template = str_replace($search,$replace,$this->template); ?> |
以上就介紹了 一個PHP模板,主要想體現一下思路,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。