茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1729117 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_315.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

PHP特區 : [轉貼]smarty的基本語法

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]smarty的基本語法
smarty的基本語法

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(王頌元)
轉載請註明出處,謝謝合作
前一個主題 | 下一個主題 | | | |

討論串




Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|