首頁  >  文章  >  後端開發  >  一個PHP模板,主要想體現一下思路

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

WBOY
WBOY原創
2016-07-29 08:35:501230瀏覽
思路:
欲在速度和易用(主要指的是美工设计的方便性)之间取得一个平衡点.于是采用了由html文件生成php文件的办法(编译?)
也想在分离显示逻辑和分离html代码之间平衡一下
例如一个论坛首页(index.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}","");
      $tpl->assign_block("{/block_cat}","");
        $tpl->assign_block("{block_forum}","\nif(\$forum['forum_cat_id'] == \$cat['forum_id']) {?>");
       $tpl->assign_block("{/block_forum}","");
      //生产PHP模板文件.
      $tpl->write_cache($tpl_index);
   }
}
//包含PHP模板文件.
include($tpl->parse_tpl($tpl_index));
?>

对应的html模板文件(index.html):
代码:

{block_cat}

 
   
 
{block_forum}
 
   
 
{/block_forum}
{=$cat['forum_name']}
{=$forum['forum_name']}



{/block_cat}

经过处理,里面的{block_forum}{block_cat}标签被替换成PHP循环语句,用于显示数组种所有元素.
生成的PHP模板文件(default_index.php):
代码:



 
   
 
if($forum['forum_cat_id'] == $cat['forum_id']) {?>
 
   
 
}?>





default_index.php被包含在index.php,這樣就可以正常顯示了.
這樣,HTML模板文件可以用dw來進行修改美化,美工人員應該會方便一些.

template.php
代碼:

/**************************************************** *****************************
*                                       *    最後修改時間:2004.4.07    本論壇使用   
*   
*
*
********************************* *************************************************/
class Template {
   //$this->$template,儲存範本資料.
   var $template = '';
   ///範本路徑.
   var $tpl_path = '';
   //範本前綴(樣式名稱).
   var $tpl_prefix = '';
    //cacheache(編譯後的路徑).
   var $cache_path = '';
   //css檔案路徑.
   var $css_path = '';
   //header檔案路徑.
''; //footer檔案路徑
    var $footer_path = '';
   /**
   * 初始化範本路徑.
   */
   function Template($root = 'default (樣式名稱).
      $this->tpl_prefix = $root;
      //範本檔案路徑.
      $this->tpl_path = './templates/  $this->d_path = './templates/  $ 🎜>      //產生的PHP檔案存放路徑.
      $this->cache_path = './template_data/' .$this->tpl_prefix . '_'; /**
   * chk_cache,檢查"編譯"後的範本是否需要更新,判斷依據:最後修改時間,"編譯"檔案是否存在.
   */
   function chk_cache($tpl_index)
    {
      $ >cache_path . $tpl_index . '.php';
      //判斷是否需要更新.
      if(!file_exists($cache_file))       }
elseif(filemtime($tpl_file) > filemtime($cache_file))
        {
         return true;          return true;
   function parse_tpl($ tpl_index,$message='')
    {
       return $this->cache_path . $tpl_index . '.php';
    }
 . ($tpl_index)
    {
      $tpl_file = $this->tpl_path . $tpl_index . '.html';
     1$fp = ffile$t_p. >template = fread($fp, filesize($tpl_file));
      fclose($fp);
   }
   /**
   * 輸出範本檔.
   */     {
      $cache_file = $this->cache_path . $tpl_index . '.php';
      //變數顯示。 ?)(})/is", "=\2?>", $this->template);
      //介面語言替換.
      $this->template = preglang_replace("/{ +(.+?)}/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)
    {
      $this->template = str_replace($search,$replace,$this->template);

?>

以上就介紹了 一個PHP模板,主要想體現一下思路,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn