smarty安裝及例子,smarty安裝例子
環境:
smarty
1.在http://www.smarty.net/download下載最新smarty包,window選擇zips,linux下選擇tar.gz。以windows為例,下載後解壓,如f:\smarty。
2.把解壓出來的smarty目錄裡lib目錄拷貝到test裡,重新命名為smarty。在test目錄下,建立tpls目錄,在tpls目錄下,建立templates、templates_c、configs、cache目錄,這幾個目錄分別是模板目錄(必要),解析目錄(必要),配置目錄(可選),緩衝目錄(可選),
smarty的php代碼和這四個目錄是同一個級的,html代碼放在templates下。
分類樹如下
代碼部分:
1.在test/smarty下建立utf-8無bom格式的main.php,配置smarty的一些成員屬性。
1 template_dir = SMARTY_ROOT."/templates/";//設定模板檔案的存放目錄 6 $tpl->compile_dir = SMARTY_ROOT."/templates_c/";//設定編譯檔案的存放目錄 7 $tpl->config_dir = SMARTY_ROOT."/configs/";//設定設定檔的存放目錄 8 $tpl->cache_dir = SMARTY_ROOT."/cache/";//設定快取檔案的存放目錄 9 $tpl->caching=1;//開啟緩衝10 $tpl->cache_lifetime=60*60*24;//有效時間為一天11 $tpl->left_delimiter = '[';//smarty語言的左右結束符12 $tpl->right_delimiter = ']';13 ?>
我們知道大括弧是smarty的預設定界符,但在和javascript、css等結合時可能會產生衝突,所以這裡我們設定為[和]。
2.在test/tpls/templates下面建立html.tpl模板檔案,就是在html中加入smarty變數。改模板相當於表現層。
html.tpl的代碼如下:
1 2 3 4 5 [$title] 6 7 8 9 [$content]10 11
3.在test目錄下建立smarty.php,該檔案相當於驅動層,給上面表現層的變數賦好值,然後顯示出來。
smarty.php的代碼如下:
1 assign("title","遲到");4 $tpl->assign("content","罰款500元!");5 $tpl->display("tpls/templates/html.tpl");6 ?>
4.在瀏覽器中運行smarty.php即可。
http://www.bkjia.com/PHPjc/1093865.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1093865.htmlTechArticlesmarty安裝及例子,smarty安裝例子 環境: smarty 1.在http://www.smarty.net/download下載最新smarty包,window選擇zips,linux下選擇tar.gz。以windows為例,下...