smarty下載:
建議使用一個兼容性好的smary版本。
太新的版本往往對php的版本支持不好。
php推薦使用的模板是:smarty
其他模板:phplib
公司自己寫的模板
如何配置我們的smarty?
1.解壓后,把libs文件夾放在網站根目錄下。
2.創建兩個文件夾 templates(放模板文件) 和templates_c(放編譯后的俄文件)
開始使用smarty
1.寫一個模板文件(tpl),通常放在templates
2.在php文件中引入smarty的核心文件,並創建smarty對象:
1 //創建smarty對象 2 require_once './libs/Smarty.class.php';
3 $smarty = new Smarty;//建立smarty實例對象$smarty
把數據分配給smarty對象,給tpl文件分配要顯示的結果集合。對象,數組。
1 //把$res分配到smarty對象 2 $smarty->assign("myArr",$arr); 3 //指定用哪個模版顯示 4 $smarty->display("empList2.tpl");
初始化其他參數(這些初始化應該放在display函數上面)
1 $smarty -> caching = false;//是否使用緩存 2 $smarty -> template_dir = "./templates";//設置模板目錄 3 $smarty -> compile_dir = "./templates_c";//設置編譯目錄 4 $smarty -> cache_dir = "./smarty_cache";//緩存文件夾 5 //修改左右邊界符號 6 $smarty -> left_delimiter="<{"; 7 $smarty -> right_delimiter="}>";
完整的代碼應該是:
1 <?php 2 3 //創建smarty對象 4 require_once './libs/Smarty.class.php'; 5 6 $smarty = new Smarty;//建立smarty實例對象$smarty 7 $smarty -> caching = false;//是否使用緩存 8 $smarty -> template_dir = "./templates";//設置模板目錄 9 $smarty -> compile_dir = "./templates_c";//設置編譯目錄 10 $smarty -> cache_dir = "./smarty_cache";//緩存文件夾 11 //修改左右邊界符號 12 $smarty -> left_delimiter="<{"; 13 $smarty -> right_delimiter="}>"; 14 15 $smarty -> assign("var1","hello world");// 16 $smarty -> display("hello.tpl");// 17 18 ?>
3.一般在控制器中用。創建一個smarty對象。require_once
注意事項:
1.替換變量的標識符一般使用:<{}>
因為{}會和內聯css和js發生沖突。
改動分隔符有兩個方法:
1直接該smarty源碼。
2 動態修改分隔符
$smarty = new Smarty;
$smarty ->left_delimiter="<{";
$smarty ->right_delimiter="}>";
傳統方法的弱點:
1、php腳本和html,css,js混合,界面不簡潔、
2.編寫php頁面要求程序員同時會網頁設計技術、
3,不利於項目分工協同開發。
smarty的用處:
1。把界面需要的數據獲取到,然后分配給某個界面。