一個PHP模板,主要想體現(xiàn)一下思路
更新時間:2006年12月25日 00:00:00 作者:
思路:
欲在速度和易用(主要指的是美工設(shè)計的方便性)之間取得一個平衡點(diǎn).于是采用了由html文件生成php文件的辦法(編譯?)
也想在分離顯示邏輯和分離html代碼之間平衡一下
例如一個論壇首頁(index.php):
對應(yīng)的html模板文件(index.html):
經(jīng)過處理,里面的{block_forum}{block_cat}標(biāo)簽被替換成PHP循環(huán)語句,用于顯示數(shù)組種所有元素.
生成的PHP模板文件(default_index.php):
default_index.php被包含在index.php,這樣就可以正常顯示了.
這樣,HTML模板文件可以用dw來進(jìn)行修改美化,美工人員應(yīng)該會方便一些.
template.php
欲在速度和易用(主要指的是美工設(shè)計的方便性)之間取得一個平衡點(diǎn).于是采用了由html文件生成php文件的辦法(編譯?)
也想在分離顯示邏輯和分離html代碼之間平衡一下
例如一個論壇首頁(index.php):
代碼: |
<?php require('./template.php'); //由html生成的php文件的前綴,區(qū)別使用多種風(fēng)格. $tpl_prefix = 'default'; //模板文件名 $tpl_index = 'index'; $tpl = new Template($tpl_prefix); $cats = array( array('forum_id'=>'1','forum_cat_id'=>'0','forum_name'=>'PHP學(xué)習(xí)'), array('forum_id'=>'2','forum_cat_id'=>'0','forum_name'=>'MYSQL學(xué)習(xí)') ); $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相關(guān)資料') ); if ($cats) { if ($tpl->chk_cache($tpl_index))//檢查判斷是否需要重新生產(chǎn)PHP模板文件. { $tpl->load_tpl($tpl_index);//加載html模板文件. //替換PHP語句 $tpl->assign_block("{block_cat}","<?foreach(\$cats as \$cat) {?>"); $tpl->assign_block("{/block_cat}","<?}?>"); $tpl->assign_block("{block_forum}","<?foreach(\$forums as \$forum) { \nif(\$forum['forum_cat_id'] == \$cat['forum_id']) {?>"); $tpl->assign_block("{/block_forum}","<?}\n}?>"); //生產(chǎn)PHP模板文件. $tpl->write_cache($tpl_index); } } //包含PHP模板文件. include($tpl->parse_tpl($tpl_index)); ?> |
對應(yīng)的html模板文件(index.html):
代碼: |
{block_cat} <table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#000000" align="center"> <tr align="{=TR_ALING}" bgcolor="#FFFFFF"> <td colspan="6"><span class="title"><b>{=$cat['forum_name']}</b></span></td> </tr> {block_forum} <tr bgcolor="#FFFFFF"> <td valign="top">{=$forum['forum_name']}</td> </tr> {/block_forum} </table> <br> {/block_cat} |
經(jīng)過處理,里面的{block_forum}{block_cat}標(biāo)簽被替換成PHP循環(huán)語句,用于顯示數(shù)組種所有元素.
生成的PHP模板文件(default_index.php):
代碼: |
<?foreach($cats as $cat) {?> <table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#000000" align="center"> <tr align="<?=TR_ALING?>" bgcolor="#FFFFFF"> <td colspan="6"><span class="title"><b><?=$cat['forum_name']?></b></span></td> </tr> <?foreach($forums as $forum) { if($forum['forum_cat_id'] == $cat['forum_id']) {?> <tr bgcolor="#FFFFFF"> <td valign="top"><?=$forum['forum_name']?></td> </tr> <?} }?> </table> <br> <?}?> |
default_index.php被包含在index.php,這樣就可以正常顯示了.
這樣,HTML模板文件可以用dw來進(jìn)行修改美化,美工人員應(yīng)該會方便一些.
template.php
代碼: |
<?php /********************************************************************************* * 模板類(Template) * 最后修改時間:2004.4.07 本論壇使用 * * * **********************************************************************************/ class Template { //$this->$template,儲存模板數(shù)據(jù). var $template = ''; //模板路徑. var $tpl_path = ''; //模板前綴(風(fēng)格名稱). var $tpl_prefix = ''; //cache路徑(編譯后的路徑). var $cache_path = ''; //css文件路徑. var $css_path = ''; //header文件路徑. var $header_path = ''; //footer文件路徑 var $footer_path = ''; /** * 初始化模板路徑. */ function Template($root = 'default') { //模板前綴(風(fēng)格名稱). $this->tpl_prefix = $root; //模板文件路徑. $this->tpl_path = './templates/' . $root . '/'; //生成的PHP文件存放路徑. $this->cache_path = './template_data/' .$this->tpl_prefix . '_'; return true; } /** * chk_cache,檢查"編譯"后的模板是否需要更新,判斷依據(jù):最后修改時間,"編譯"文件是否存在. */ 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); } } ?> |
相關(guān)文章
php中sort函數(shù)排序知識點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是一篇關(guān)于php中sort函數(shù)排序知識點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以參考下。2021-01-01php操作xml入門之xml基本介紹及xml標(biāo)簽元素
這篇文章主要介紹了php操作xml入門之xml基本介紹及xml標(biāo)簽元素,實(shí)例分析了XML的組成、規(guī)范與用法,需要的朋友可以參考下2015-01-01PHP實(shí)現(xiàn)鏈?zhǔn)讲僮鞯娜N方法詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)鏈?zhǔn)讲僮鞯娜N方法,結(jié)合實(shí)例形式分析了php鏈?zhǔn)讲僮鞯南嚓P(guān)實(shí)現(xiàn)技巧與使用注意事項(xiàng),需要的朋友可以參考下2017-11-11php獲取$_POST同名參數(shù)數(shù)組的實(shí)現(xiàn)介紹
本篇文章是對php獲取$_POST同名參數(shù)數(shù)組的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP+mysql實(shí)現(xiàn)的三級聯(lián)動菜單功能示例
這篇文章主要介紹了PHP+mysql實(shí)現(xiàn)的三級聯(lián)動菜單功能,涉及mysql數(shù)據(jù)庫創(chuàng)建、數(shù)據(jù)添加及php讀取mysql、創(chuàng)建聯(lián)動菜單相關(guān)操作技巧,需要的朋友可以參考下2019-02-02PHP中冒號、endif、endwhile、endfor使用介紹
下面的一些用法,對于相當(dāng)一部分PHP愛好者來說根本沒見過啊,這些是什么東西呢?2010-04-04PHP實(shí)現(xiàn)服務(wù)器狀態(tài)監(jiān)控的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)服務(wù)器狀態(tài)監(jiān)控的方法,可實(shí)現(xiàn)對指定IP服務(wù)器狀態(tài)的有效監(jiān)控,非常具有實(shí)用價值,需要的朋友可以參考下2014-12-12