Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

if,elseif,else

Comandos {if} no Smarty tem muito da mesma flexibilidade do php, com algumas características à mais para o sistema de template. Todo if deve ter o seu /if. else e elseif também são permitidos. Todos os condicionais do PHP são reconhecidos, tais como ||, or, &&, and, etc.

A seguir está uma lsita dos qualificadores, que devem estar separados dos elementos por espaço. Note que itens listado entre [conchetes] são opcionais. Os equivalentes em PHP são mostrados quando aplicáveis.

Qualificador Alternativas Exemplo de sintaxe Significado Equivalente no PHP
== eq $a eq $b iguais ==
!= ne, neq $a neq $b não iguais !=
> gt $a gt $b maior que >
< lt $a lt $b menor que <
>= gte, ge $a ge $b maior ou igual à >=
<= lte, le $a le $b menor ou igual à <=
! not not $a negação (unary) !
% mod $a mod $b módulo %
is [not] div by   $a is not div by 4 divisível por $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] an odd grouping ($a / $b) % 2 != 0

Example 7.11. comandos if


{if $name eq "Fred"}
	Welcome Sir.
{elseif $name eq "Wilma"}
	Welcome Ma'am.
{else}
	Welcome, whatever you are.
{/if}

{* um exemplo com "or" *}
{if $name eq "Fred" or $name eq "Wilma"}
	...
{/if}

{* o mesmo que acima *}
{if $name == "Fred" || $name == "Wilma"}
	...
{/if}

{* a seguinte sintaxe não irá funcionar, qualificadores de condição
   devem estar separados dos elementos em torno por espaços *}
{if $name=="Fred" || $name=="Wilma"}
	...
{/if}


{* parenteses são permitidos *}
{if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#}
	...
{/if}

{* você pode também colocar funções php *}
{if count($var) gt 0}
	...
{/if}

{* testa se o valor é par ou impar *}
{if $var is even}
	...
{/if}
{if $var is odd}
	...
{/if}
{if $var is not odd}
	...
{/if}

{* verifica se a variável é divisível por 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}