例如一個(gè)論壇首頁(yè)(index.php): 代碼:<?php r " /> 国产午夜亚洲精品理论片八戒,精品国产免费人成视频,97人妻无码AV碰碰视频

天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

一個(gè)PHP模板,主要想體現(xiàn)一下思路

思路:
欲在速度和易用(主要指的是美工設(shè)計(jì)的方便性)之間取得一個(gè)平衡點(diǎn).于是采用了由html文件生成php文件的辦法(編譯?)
也想在分離顯示邏輯和分離html代碼之間平衡一下

例如一個(gè)論壇首頁(yè)(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高級(jí)教程'),
   array('forum_id'=>'4','forum_cat_id'=>'1','forum_name'=>'php初級(jí)教程'),
   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語(yǔ)句
      $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));
?>

對(duì)應(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)過(guò)處理,里面的{block_forum}{block_cat}標(biāo)簽被替換成php循環(huán)語(yǔ)句,用于顯示數(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來(lái)進(jìn)行修改美化,美工人員應(yīng)該會(huì)方便一些.

template.php
代碼:

<?php
/*********************************************************************************
*                                                                 模板類(lèi)(Template)
*    最后修改時(shí)間:2004.4.07    本論壇使用   
*   
*
*
**********************************************************************************/
class Template {

   //$this->$template,儲(chǔ)存模板數(shù)據(jù).
   var $template = '';

   //模板路徑.
   var $tpl_path = '';

   //模板前綴(風(fēng)格名稱(chē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)格名稱(chēng)).
      $this->tpl_prefix = $root;
      //模板文件路徑.
      $this->tpl_path = './templates/' . $root . '/';
      //生成的php文件存放路徑.
      $this->cache_path = './template_data/' .$this->tpl_prefix . '_';
      return true;
   }

   /**
   * chk_cache,檢查"編譯"后的模板是否需要更新,判斷依據(jù):最后修改時(shí)間,"編譯"文件是否存在.
   */
   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);

      //界面語(yǔ)言替換.
      $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技術(shù)一個(gè)PHP模板,主要想體現(xiàn)一下思路,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 狠狠色丁香婷婷久久综合 | 香蕉精品国产高清自在自线 | 亚洲精品资源网在线观看 | 男人的天堂久久精品激情a 男人的天堂黄色片 | 里番※琉璃全彩acg奈亚子 | 欧美一夜爽爽爽爽爽爽 | 亚洲国产精品一区二区动图 | 99视频国产热精品视频 | 精品一区二区三区色花堂 | 日韩人妻少妇一区二区三区 | 亚洲电影二区 | 欧美日韩中文国产一区 | 歪歪爽蜜臀AV久久精品人人槡 | JIZZ19学生第一次 | 国产精品 中文字幕 亚洲 欧美 | 蜜桃最新网址 | 51国产偷自视频在线视频播放 | 久久成人免费大片 | 日韩亚洲不卡在线视频 | 老头扒开粉缝亲我下面 | 夜月视频直播免费观看 | 久久久视频2019午夜福利 | 国产三级在线精品男人的天堂 | 国产精品自在在线午夜蜜芽tv在线 | a一级毛片视频免费看 | 超碰97人在线视频 | 男人团apk| 性绞姿始动作动态图 | 曰本老头同性xxxxx | 北岛玲手机在线观看视频观看 | 亚洲成A人片在线观看中文不卡 | 国产69精品久久久久人妻刘玥 | 老师的快感电影完整版 | 婚后被调教当众高潮H喷水 回复术士勇者免费观看全集 | 野花韩国在线观看 | 国产色婷亚洲99精品AV在线 | 一区二区三区四区国产 | 免费亚洲视频 | 伊人色综合久久天天 | 亚洲乱码AV久久久久久久 | 国产色婷亚洲99精品AV |