Smarty 图标

你可以根据商标声明使用 Smarty 徽标。

Smarty Template Engine Smarty Template Engine

对于赞助、广告、新闻或其他询问,请通过以下方式联系我们

使用 Smarty 的网站

广告

{if}{elseif}{else}

{if} 语句在 Smarty 中与 PHP if 语句具有非常相似的灵活性,并添加了一些针对模板引擎的功能。每个 {if} 都必须与匹配的 {/if} 配对使用。还可以使用 {else}{elseif}。它识别所有 PHP 条件和函数,例如 ||or&&andis_array() 等。

如果启用了安全性,则仅允许安全策略的 $php_functions 属性中的 PHP 函数。有关详细信息,请参阅安全性部分。

以下是已识别标识符列表,这些标识符必须用空格与周围元素分开。请注意,[方括号] 中列出的项目是可选的。在适用的情况下显示 PHP 等效项。

标识符 其他选项 语法示例 含义 PHP 等效项
== eq $a eq $b equals ==
!= ne, neq $a neq $b not equals !=
> gt $a gt $b greater than >
< lt $a lt $b less than <
>= gte, ge $a ge $b greater than or equal >=
<= lte, le $a le $b less than or equal <=
===   $a === 0 check for identity ===
! not not $a negation (unary) !
% mod $a mod $b modulous %
is [not] div by   $a is not div by 4 divisible by $a % $b == 0
is [not] even   $a is not even [not] an even number (unary) $a % 2 == 0
is [not] even by   $a is not even by $b grouping level [not] even ($a / $b) % 2 == 0
is [not] odd   $a is not odd [not] an odd number (unary) $a % 2 != 0
is [not] odd by   $a is not odd by $b [not] 一个奇数分组 ($a / $b) % 2 != 0

示例 7.44。{if} 语句

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

  

示例 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}