smarty基础
smarty基礎原理
一、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>二、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");三、模板引擎
<?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");}}四、混編代碼頁面
<!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>?
轉載于:https://www.cnblogs.com/m-m-g-y0416/p/5700081.html
總結
- 上一篇: 虚拟机无法开机数据恢复 (建议在做之
- 下一篇: 【CV论文阅读】 Fast RCNN +