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

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_4471.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(王頌元)
轉載請註明出處,謝謝合作
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]smarty if,elseif,else 新語法
smarty if,elseif,else 新語法

if,elseif,else
if 語句和和條件同 php 差不多,但每個詞之間必須用空格分割開。
也有一些新的條件語句,列舉如下:
eq相等,
ne、neq不相等,
gt大於,
lt小於,
gte、ge大於等於,
lte、le 小於等於,
not非,mod求模。
is [not] div by是否能被某數整除,
is [not] even是否為偶數,
$a is [not] even by $b即($a / $b) % 2 == 0,
is [not] odd是否為奇,
$a is not odd by $b即($a / $b) % 2 != 0



原文出處:smarty if,elseif,else 新语法 - 程序人生 提供数据深度挖掘服务 - 博客频道 - CSDN.NET
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Smarty 判斷式語法
Smarty判斷式語法

Smarty if 基本語法
{if 判斷句}
區塊1
{else if 判斷句}
區塊2
{else}
區塊3
{/if}


修飾字
== 等於
!= 不等於
> 大於
< 小於
>= 大於等於
<= 小於等於
=== 值與型態皆相等
! not
% 取餘數

is [not] div by 是否能被整除
is [not] even 是否是偶數
is [not] odd 是否是單數




原文出處:我的IT筆記: Smarty判斷式語法
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]php smarty if語句的使用方法實例
php smarty if語句的使用方法實例
時間:2010-05-13 17:48 來源:原創 作者:村長

smarty提供了幾種控制結構,能夠按條件和迭代處理傳入的數據。

1.if函數
smarty的if函數與php語言中的if函數相同。
與php一樣,可以使用一些條件限定符,如下所示:
‧eq----is not odd
‧gt----div by
‧gte----even by
‧ge-----not
‧lt-----mod
‧lte----odd by
‧le----==
‧ne----!=
‧neq--- >
‧is even---- <
‧is not even----  <=
‧is odd----        >=

下面是一個簡單實例:
{*Assume $dayofweek=6. *}
{if $dayofweek>5}
<p>Gotta love the weekend!</p>
{/if}

再考慮另一個示例。假設希望根據月份插入某個消息。如下示例使用了條件限定符、elseif以及else語句完成這個任務:
{if $month<4}
Summer is coming!
{elseif $month ge 4 && $month <=9}
it's hot out today!
{else}
brrr...it's cold!
{/if}

注意,把條件語句包圍在小括號中可選的,但在標準PHP代碼中這卻是必需的。



原文出處:php smarty if语句的使用方法实例_网站源代码
前一個主題 | 下一個主題 | 頁首 | | |



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