第一二九上課 PHP 自製簡單開發模板(1)

來源:互聯網
上載者:User

標籤:

構建基礎架構

在專案檔夾(自訂)下建立

(1)核心目錄:WQ

(2)模板目錄:MoBan

(3)編譯目錄:BianYi

(4)建立設定檔: config.ini.php

<?php//擷取檔案所在目錄$Gen=dirname(__FILE__);//設定模板目錄define(‘MoBan‘,$Gen.‘/MoBan/‘);//設定核心類目錄define(‘WQ‘,$Gen.‘/WQ/‘);//設定編譯檔案目錄define(‘BianYi‘,$Gen.‘/BianYi/‘);//載入核心類include WQ.‘WQ.class.php‘;//調用核心類$wq=new WQ();

(5)在核心目錄WQ下建立系統核心類WQ.class.php

<?php//系統核心類class WQ{    //變數成員(一個可以放下無數變數的數組)    private $BianLiang = array();        //構造方法    public function __construct()    {        if(!is_dir(MoBan) || !is_dir(BianYi))        {            exit("模板目錄:MoBan 或 編譯目錄:BianYi 不存在!請手動建立");        }    }    //註冊變數的方法    public function assign($key,$value)    {        if(empty($key))        {            exit("錯誤:註冊變數名不可為空");        }        $this->BianLiang["$key"]=$value;        }        //調用模板的方法    public function display($MoBanMing)    {        //擷取模板路徑        $MoBanLuJing=MoBan.$MoBanMing;                //容錯,當模板不存在時        if(!file_exists($MoBanLuJing))        {            exit(‘錯誤:模板檔案不存在‘);        }                //編譯檔案路徑        $BianYiLuJing=BianYi.md5($MoBanMing).$MoBanMing.‘.php‘;                //產生編譯檔案        if(!file_exists($BianYiLuJing) || filemtime($BianYiLuJing)<filemtime($MoBanLuJing))        {            //引入解析類檔案,執行個體化解析類並調用解析方法            include WQ.‘JieXi.class.php‘;                $JieXi= new JieXi($MoBanLuJing);            $JieXi->jiexi($BianYiLuJing);            }                //載入編譯檔案        include $BianYiLuJing;        }    }

(5)在核心目錄WQ下建立模板解析類JieXi.class.php

<?php//模板解析類class JieXi{    private $MoBanNeiRong;        //構造方法    public function __construct($MoBanLuJing)    {        if(!$this->MoBanNeiRong=file_get_contents($MoBanLuJing))        {            exit(‘錯誤:模板檔案讀取錯誤‘);        }    }        //對外公用方法    public function jiexi($BianYiLuJing)    {        //調用普通變數解析法        $this->jiexi_bianliang();                //調用foreach標籤解析法        $this->jiexi_foreach();                //產生編譯檔案        if(!file_put_contents($BianYiLuJing,$this->MoBanNeiRong))        {            exit(‘錯誤:編譯檔案產生失敗‘);        }    }        //解析普通變數    private function jiexi_bianliang()    {        //賦值變數解析正則        $ZhengZe=‘/<\{\$([\w]+)\}>/‘;        if(preg_match($ZhengZe,$this->MoBanNeiRong))        {            $TiHuan="<?php echo \$this->BianLiang[‘$1‘] ?>";            //替換解析模板變數            $this->MoBanNeiRong=preg_replace($ZhengZe,$TiHuan,$this->MoBanNeiRong);        }    }        //解析foreach標籤    private function jiexi_foreach()    {        //賦值解析正則        $ZhengZe="/<\{foreach\s+([\w]+)\(([\w]+),([\w]+)\)\}>/";        $ZhengZe_end="/<\{\/foreach\}>/";        $ZhengZe_nei="/<\{([\w]+)\}>/";        if(preg_match($ZhengZe,$this->MoBanNeiRong))        {            if(preg_match($ZhengZe_end,$this->MoBanNeiRong))            {                //替換模板標籤foreach                $this->MoBanNeiRong=preg_replace($ZhengZe_end,‘<?php } ?>‘,$this->MoBanNeiRong);                $TiHuan="<?php foreach(\$this->BianLiang[‘$1‘] as \$$2 => \$$3){ ?>";                $this->MoBanNeiRong=preg_replace($ZhengZe,$TiHuan,$this->MoBanNeiRong);                    if(preg_match($ZhengZe_nei,$this->MoBanNeiRong))                {                    //替換foreach標籤索引及變數                    $TiHuan="<?php echo \$$1 ?>";                    $this->MoBanNeiRong=preg_replace($ZhengZe_nei,$TiHuan,$this->MoBanNeiRong);                }                }            else            {                exit("錯誤:foreach標籤沒有封閉");            }        }    }    }

 

第一二九上課 PHP 自製簡單開發模板(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.