php與html分離程式

來源:互聯網
上載者:User

 

php與html分離程式

<?php
// 追月的小蛤蟆 QQ:695703951     不知道那個師父收小弟..有個人指點.學得快... 
class fc_view{
        var $fc_dir;                                //指定目錄
        var $fc_file;                             //視圖檔案
        var $fc_aval        =array();        //數組
        var $fc_lemark         ='{';                //左標記符號 你可以自由更改,如##.等等符號
        var $fc_rimark         ='}';                //右標記符號
        
        //指定模板目錄
        function dir($d){
                $this->fc_dir = $d;
                if (file_exists($this->fc_dir)) {   //檢查是否存在目錄.如果假.回上一級目錄
                        $this->fc_dir = dirname($PATH).$d.'/'; 
                }else{
                        $this->fc_dir = '../'.dirname($PATH).$d.'/';
                }
                }
        //指定模板檔案
        function file($f){
                $this->fc_file = $f;
        }
        function ophtm($var){
                $file =@file_get_contents($this->fc_dir.$this->fc_file) or die ("沒有找到:".
                $this->fc_dir.$this->fc_file."檔案");
                foreach ($var as $key=>$val){
                $file = str_replace($this->fc_lemark.$key.$this->fc_rimark,$val,$file); //替換
                }
                print $file;
        }
}
?>

使用
-------------------------------------------------------

複製PHP內容到剪貼簿
PHP代碼:

<?php
include('Common/fc_view.php'); //運行指定檔案 .就是模板檔案
$view = new fc_view();    //類
$view->dir('htmview'); //模板目錄
$view->file('index.html'); //模板檔案
$var =array();        //指定數組;
$var['title']= "標題"; //替換html檔案下的{title} 
$var['main'] = "共用..嘻嘻.大家用來學習.<br>追月的小蛤蟆<br>";//替換html檔案下的{main} 
if (file_exists('index.php1')) {
$var['hehe'] = "找到了";
}else{
$var['hehe'] = "沒有找到檔案";
}
$view->ophtm($var); //輸出模板
?>

-------------------------------------------------------




-------------------------------------------------------
HTML檔案
-------------------------------------------------------

複製PHP內容到剪貼簿
PHP代碼:
<html>
<head>
<title>{title}</title>
</head>
<body>
{main}
{hehe}
</body>
</html>
相關文章

聯繫我們

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