PHP smarty Template conditional selection structure If-elseif-else,{if} must be paired with {/if}, but you can also use {else} and {ElseIf} clauses, and the following modifiers can be used in {if}:
If instance
{if} instance:
{if $name eq ' Fred '} Welcome Sir. {elseif $name eq ' Wilma '} Welcome Ma ' am. {Else} Welcome, whatever. {/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 is allowed *} {if ($amount < 0 or $amount >) 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. 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}
If ElseIf instance:
{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}
Instance
<table> {foreach from= $users item=row} <tr> {foreach from= $row item=col key=k} {* Note the space between the IF statement and the parentheses (not required), there is a strict requirement *} <TD {if ($col = = ' wjj ')}style= ' color:red; ' {ElseIf ($col = = ' Qxy ')}style= ' color:green; ' {/if}>{$k}:{$col}</td> {/foreach} </tr> {foreachelse} <tr><td> No data. </td></tr> {/foreach}</table>
The program assigns the $users variable to the following array:
array (Array (' name ' = = ' wjj ', ' age ' = ' confidential '), Array (' name ' = = ' Q XY ', ' age ' = ' seems smaller than me ')