smart基礎原理

來源:互聯網
上載者:User

標籤:

1html模板頁面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title></head><body><div>{$title}</div><div>{$content}</div></body></html> 

2.php後台頁面

<?php//串連資料庫,獲得具體資料//1.引入迷你smartrequire ("minismart.class.php");//2執行個體化Smarty對象$smarty=new minismart();//將字串資訊設定為模板引擎類的屬性資訊$smarty->assign("title","qqq");$smarty->assign("content","aa");//3調用compile方法,同時傳遞ceshi.html模板檔案參數//在該方法中把ceshi.html內部標記替換為php標記$smarty->compile("ceshi.html");

3模板引擎

<?php//模板引擎類class minismart {      //給該類聲明屬性,用於儲存外部的變數資訊    public $tpl_var=array();    //把外部變數設定成內部變數的一部分    function assign($k,$v)    {     $this->tpl_var[$k]=$v;    }     //"編譯模板檔案({}標記替換為php標記)"    function compile ($tpl) //compile編譯    {  //獲得模板檔案內部具體內容       $cont= file_get_contents($tpl);//file_get_contents()擷取內容             //替換 { ---> <?php echo $this->tpl_var["    $cont=str_replace("{\$","<?php echo \$this->tpl_var[\"",$cont);        //替換 } --->"]; ? >    $cont=str_replace("}","\"]; ?>",$cont);          //把產生好的編譯內容(php + html 混編內容)放入一個檔案裡面    file_put_contents("./shili.html.php",$cont);      //引入混編檔案    include ("./shili.html.php");    }    }

4混編檔案

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>無標題文檔</title></head><body><div><?php echo $this->tpl_var["title"]; ?></div><div><?php echo $this->tpl_var["content"]; ?></div></body></html> 

 

smart基礎原理

聯繫我們

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