標籤:
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基礎原理