ECSHOP模板系統控制標簽介紹說明,本文將為您介紹ecshop中基本的控制函數標簽的使用參數和方法,其中包括if標簽、foreach標簽、for標簽等,其實Smarty 中的 if 語句和 php 中的 if 語句一樣靈活易用,并增加了幾個特性以適宜模板引擎, if必須于/if 成對出現. 可以使用 else 和 elseif 子句。
if,elseif,else
描述:
Smarty 中的 if 語句和 php 中的 if 語句一樣靈活易用,并增加了幾個特性以適宜模板引擎. if必須于 /if 成對出現. 可以使用 else 和 elseif 子句. 可以使用以下條件修飾詞:eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、!=、>、<、<=、>=. 使用這些修飾詞時必須和變量或常量用空格格開。
例子:
{if $name eq "Fred"} Welcome Sir. {elseif $name eq "Wilma"} Welcome Ma'am. {else} Welcome, whatever you are. {/if} {* an example with "or" logic *} {if $name eq "Fred" or $name eq "Wilma"} ... {/if} {* same as above *} {if $name == "Fred" || $name == "Wilma"} ... {/if} {* the following syntax will NOT work, conditional qualifiers must be separated from surrounding elements by spaces *} {if $name=="Fred" || $name=="Wilma"} ... {/if} {* parenthesis are allowed *} {if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#} ... {/if} {* you can also embed php function calls *} {if count($var) gt 0} ... {/if} {* test if values are even or odd *} {if $var is even} ... {/if} {if $var is odd} ... {/if} {if $var is not odd} ... {/if} {* test if var is divisible by 4 *} {if $var is div by 4} ... {/if} {* test if var is even, grouped by two. i.e., 0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, etc. *} {if $var is even by 2} ... {/if} {* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *} {if $var is even by 3} ... {/if}
foreach,foreachelse
iteration:
iteration 用于顯示當前循環的執行次數[待考]
iteration 總是從 1 開始,每執行一次增加 1.[待考]
first:
當前 foreach 循環第一次執行時 first 被設置成 true.
last:
當前 foreach 循環執行到最后一遍時 last 被設置成 true.
show:
show 是 foreach 的一個參數. 取值為布爾值 true 或 false. 如果指定為 false 該循環不顯示,如果循環指定了 foreachelse 子句,該子句顯示與否也取決于 show 的取值。
total:
total 用于顯示循環執行的次數,可以在循環中或循環執行后調用。
屬性 |
類型 |
是否必須 |
缺省值 |
描述 |
from |
string |
Yes |
n/a |
待循環數組的名稱 |
item |
string |
Yes |
n/a |
當前處理元素的變量名稱 |
key |
string |
No |
n/a |
當前處理元素的鍵名 |
name |
string |
No |
n/a |
該循環的名稱,用于訪問該循環 |
描述:
foreach 是除 section 之外處理循環的另一種方案(根據不同需要選擇不同的方案)。
foreach 用于處理簡單數組(數組中的元素的類型一致),它的格式比 section 簡單許多,缺點是只能處理簡單數組。
foreach 必須和 /foreach 成對使用,且必須指定 from 和 item 屬性。
name 屬性可以任意指定(字母、數字和下劃線的組合)。
foreach 可以嵌套,但必須保證嵌套中的 foreach 名稱唯一。
from 屬性(通常是數組)決定循環的次數。
foreachelse 語句在 from 變量沒有值的時候被執行。
例子1:
{* 該例將輸出數組 $custid 中的所有元素的值 *} {foreach from=$custid item=curr_id} id: {$curr_id}<br> {/foreach}
輸出:
id: 1000<br> id: 1001<br> id: 1002<br>
例子2:
{* The key contains the key for each looped value assignment looks like this: $smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"), array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234"))); *} {* 鍵就是數組的下標,請參看關于數組的解釋 *} {foreach name=outer item=contact from=$contacts} {foreach key=key item=item from=$contact} {$key}: {$item}<br> {/foreach} {/foreach}
輸出:
phone: 1<br> fax: 2<br> cell: 3<br> phone: 555-4444<br> fax: 555-3333<br> cell: 760-1234<br>
foreach 循環有自己的變量名,使用該變量名可以訪問該循環. 使用方法為{$smarty.foreach.foreachname.varname},其中 foreachname 即在 foreach 中指定的name 屬性。