一個PHP模板,主要想體現一下思路

來源:互聯網
上載者:User

思路:
欲在速度和易用(主要指的是美工設計的方便性)之間取得一個平衡點.於是採用了由html檔案產生php檔案的辦法(編譯?)
也想在分離顯示邏輯和分離html代碼之間平衡一下

例如一個論壇首頁(index.php):

代碼:
<?php
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}","<?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}?>");
//生產PHP模板檔案.
$tpl->write_cache($tpl_index);
}
}
//包含PHP模板檔案.
include($tpl->parse_tpl($tpl_index));
?>


對應的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}


經過處理,裡面的{block_forum}{block_cat}標籤被替換成PHP迴圈語句,用於顯示數組種所有元素.

產生的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來進行修改美化,美工人員應該會方便一些.

template.php

代碼:
<?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);
}
}
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.