對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2012/11/10 6:45:04
smarty的基本語法
1.smarty的配置
首先,使用smarty第一件事是先配置好,一般有以下9行代碼
以上的9行代碼可以放在一個獨立的文件,需要使用smarty的頁面引用進來即可
2. smarty的使用
smarty替換標籤的語法:
假定模板文件裏面有一個標籤 <{ $user_name }> (註意:標籤裏面的變量必須帶$)
那麼在PHP文件可以:
3. smarty的循環
循環的使用在php文件像處理普通標籤一樣處理
模板文件代碼:
那麼在PHP文件可以:
假如有一個數組,或一個記錄集 $rs
4.smarty的if 語法
模板文件:
php文件:
上例中,如果$aa>500,my_r的標籤就能顯示出來,否則不顯示。
5.smarty循環配合if 使用實例
PHP文件:
模板文件:
上例中,只顯示大於200的數值
原文出處:
smarty的基本語法 - php網站開發 - fxc86 - 和訊博客
教程原創:www.37dg.com(王頌元)
轉載請註明出處,謝謝合作
1.smarty的配置
首先,使用smarty第一件事是先配置好,一般有以下9行代碼
require_once("smarty/libs/Smarty_class.php"); //把smarty的類定義文件包含進來
$smarty=new smarty();
$smarty->config_dir="smarty/libs/Config_File.class.php";
$smarty->caching=false; //是否使用緩存,項目在調試期間,不建議啟用緩存
$smarty->cache_dir="smarty_cache/"; //緩存文件夾
$smarty->template_dir="smarty_tpl"; //模板文件夾
$smarty->compile_dir="smarty_compile"; //編譯文件夾
$smarty->left_delimiter="<{"; // 標籤符定義不是必要的,smarty默認是使用"<"和">",強烈建議更換。
//因為如果smarty的標籤剛好在javascript語句裏面時,衝突的可能性很大
$smarty->right_delimiter="}>";
以上的9行代碼可以放在一個獨立的文件,需要使用smarty的頁面引用進來即可
2. smarty的使用
smarty替換標籤的語法:
smarty->assign("標籤名","值");
smarty->display("index.html"); //顯示內容,index為模板文件名
假定模板文件裏面有一個標籤 <{ $user_name }> (註意:標籤裏面的變量必須帶$)
那麼在PHP文件可以:
$new_name="Joan";
smarty->assign("user_name",$new_name); (註意:在此時,user_name是不帶$的)
smarty->display("index.html"); //顯示內容,index為模板文件名
3. smarty的循環
循環的使用在php文件像處理普通標籤一樣處理
模板文件代碼:
<table border=1 width=500>
<{section name=s loop=$stu}>
<tr>
<td>
<{$stu[s]}>
</td>
</tr>
<{/section}>
</table>
那麼在PHP文件可以:
假如有一個數組,或一個記錄集 $rs
$smarty->assign("stu",$rs);
$smarty->display("index.html"); //在PHP文件處理循環標籤和普通標籤沒什麼區別。
4.smarty的if 語法
模板文件:
<{if $my_r>500 }>
<{$my_r}>
<{/if}>
php文件:
$aa=123;
$smarty->assign("my_r",$aa);
$smarty->display("in.html");
上例中,如果$aa>500,my_r的標籤就能顯示出來,否則不顯示。
<{ if 條件 }> //此處的條件不要加括號,也不要像basic語言那樣在後面加 then
<{/if}>
5.smarty循環配合if 使用實例
PHP文件:
$aa[0]=123;
$aa[1]=456;
$aa[2]=789;
$smarty->assign("my_r",$aa);
$smarty->display("in.html");
模板文件:
<{ section name=s loop=$my_r }>
<{if $my_r[s]>200 }>
<{$my_r[s]}>
<{else}>
小於200
<{/if}>
<br>
<{/section}>
上例中,只顯示大於200的數值
原文出處:
smarty的基本語法 - php網站開發 - fxc86 - 和訊博客
教程原創:www.37dg.com(王頌元)
轉載請註明出處,謝謝合作