Smarty:if函數(shù)

2018-10-14 11:53 更新

{if},{elseif},{else}

Smarty的{if}條件判斷和PHP的if 非常相似,只是增加了一些特性。 每個{if}必須有一個配對的{/if}. 也可以使用{else} 和 {elseif}. 全部的PHP條件表達(dá)式和函數(shù)都可以在if內(nèi)使用,如||or&&andis_array(), 等等.

如果開啟了安全機(jī)制,那么只有在$php_functions設(shè)置允許的PHP函數(shù)才能使用。 參見安全機(jī)制。

下面是可用的運(yùn)算符列表,使用中都會放到元素的中間并且用空格分隔。 注意列表中[方括號]的是可選的,而且還會列出對應(yīng)PHP的表達(dá)式。

運(yùn)算符別名語法示例含義對應(yīng)PHP語法
==eq$a eq $b等于==
!=ne, neq$a neq $b不等于!=
>gt$a gt $b大于>
<lt$a lt $b小于<
>=gte, ge$a ge $b大于等于>=
<=lte, le$a le $b小于等于<=
=== $a === 0絕對等于===
!notnot $a非 (一元運(yùn)算)!
%mod$a mod $b取模%
is [not] div by $a is not div by 4取模為0$a % $b == 0
is [not] even $a is not even[非] 取模為0 (一元運(yùn)算)$a % 2 == 0
is [not] even by $a is not even by $b水平分組 [非] 平均($a / $b) % 2 == 0
is [not] odd $a is not odd[非] 奇數(shù) (一元運(yùn)算)$a % 2 != 0
is [not] odd by $a is not odd by $b[非] 奇數(shù)分組($a / $b) % 2 != 0

Example 7.44. {if} 表達(dá)式

{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}


{* 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}

{* check for array. *}
{if is_array($foo) }
   .....
{/if}

{* check for not null. *}
{if isset($foo) }
   .....
{/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}

Example 7.45. {if} 的更多例子

 
{if isset($name) && $name == 'Blog'}
     {* do something *}
{elseif $name == $foo}
    {* do something *}
{/if}

{if is_array($foo) && count($foo) > 0}
    {* do a foreach loop *}
{/if}

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號