例如一個論壇首頁(index.php): 代碼:<?php r " /> 玖玖爱zh综合伊人久久,免费视频成人国产精品网站,国产亚洲精品日韩综合网

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

思路:
欲在速度和易用(主要指的是美工設計的方便性)之間取得一個平衡點.于是采用了由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("http://{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);
   }
}
?>

php技術一個PHP模板,主要想體現一下思路,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 亚洲天堂久久精品成人 | 亚洲区色 | 狠狠狠地在啪线香蕉 | 四虎免费看 | 国产精品嫩草影院一二三区 | 国产精品免费福利 | 婷婷激情五月网 | 亚洲美女aⅴ久久久91 | 国产亚洲美女精品久久久久狼 | 亚洲一区二区观看 | 中文字幕曰韩一区二区不卡 | 日韩理论视频 | 日韩中文字幕亚洲无线码 | 国内精品哆啪啪 | 91麻豆国产香蕉久久精品 | 亚洲黄色高清视频 | 国产精品人伦久久 | 国产情侣普通话刺激对白 | 伊人天伊人天天网综合视频 | 精品国产品国语在线不卡丶 | 欧美专区一区二区三区 | 国产精品亚洲一区二区三区正片 | 最新国产福利在线观看 | 深爱激动网婷婷狠狠五月 | 免费在线视频一区 | 欧美激情在线看 | 成人黄色免费网址 | 精品国内视频 | 国产成人精品第一区二区 | 91麻豆精品国产91久久久久 | 国产精品99久久免费观看 | 国产黄色小视频在线观看 | 国产自产第一区c国产 | 日本欧美国产精品第一页久久 | 天天拍夜夜添久久精品免费 | 激情视频网 | 黑人干我 | 色综合成人网 | 九月丁香婷婷 | 日本免费久久久久久久网站 | 337p欧洲大胆扒开图片 |