欲在速度和易用(主要指的是美工設計的方便性)之間取得一個平衡點.於是採用了由html文件生成php文件的辦法(編譯?)
也想在分離顯示邏輯和分離html在程式碼之間平衡字首,差異使用多種樣式.
$tpl_prefix = 'default';
//範本檔案名稱
$tpl_index = 'index';
$7, Template($tpl_prefix);
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'=>'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}","foreach( $cats as $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模板檔案.
}
}
//包括PHP模板檔案.
in> parse_tpl($tpl_index));
?>
對應的html範本檔案(index.html):
碼:
. ="1" cellpadding="1" bgcolor="#000000" align="center">
{block_forum}
{/block_forum}
經過處理,裡面的{block_forum}{block_cat}標籤被替換成PHP循環語句,用於顯示數組種所有元素.
產生的PHP模板檔案(default_index.php):
代碼: foreach($cats as $cat) {?>
=$cat['forum_name']?> | |||||
=$forum['forum_name']?> |
default_index.php被包含在index.php,這樣就可以正常顯示了.
這樣,HTML模板可以用美工來修改文件可以用美工來進行修改美化,美工人員應該會方便一些.
template.php
代碼:
/**************************************************** *****************************
* 範本類別(Template)
* 最後修改時間:2004.4.07 本論壇使用
*
*
*
********************************************* *************************************/
class Template {
//$this->$template,儲存模板資料.
var $template = '';
//模板路徑. $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 './templates/' . $root . '/';
//產生的PHP檔案存放路徑.
$this->cache_path = './template_data/' .$this->tpl_prefix . '_';
return true;
}
/**
* 初始化範本路徑.
*/
function chk_cache($tpl_index)
{
$tpl_file = $this->tpl_path . $tpl 。
{
return true;
}
}
嗎
/**
* chk_cache,檢查"編譯"後的模板是否需要更新,判斷依據:最後修改時間,"編譯"檔案是否存在.
*/
function parse_tpl($tpl_index,$message='') $ ;
}
/**
* 輸出範本檔.
*/
function load_tpl($tpl_index)
{
$tpl_file = $this->tpl_path . $tpl_index . '.html'$. r');
$this->template = fread($fp, filesize($tpl_file));
fclose($fp);
}
/**
* 載入範本檔案.
*/ )$$f {
$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);
}
/**
* 替換變數,並且"編譯"模板.
*/
function assign_block($search,$replace)#&*/
function assign_block($search,$replace))> = str_replace($search,$replace,$this->template);
}
}
?>
感謝您的閱讀,想要獲得更多相關內容請關注中文網(www.php.cn)!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具