smarty基礎原理php
1、html模板頁面html
<!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.引入迷你smart require ("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、模板引擎ui
<?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、混編代碼頁面this
<!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>