mirror of
				https://github.com/smarty-php/smarty.git
				synced 2025-10-30 20:01:37 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			263 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
			
		
		
	
	
			263 lines
		
	
	
		
			6.0 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
| <?xml version="1.0" encoding="iso-8859-1"?>
 | |
| <!-- $Revision$ -->
 | |
| <sect1 id="language.function.if">
 | |
|  <title>{if},{elseif},{else}</title>
 | |
|  <para>
 | |
|   <varname>{if}</varname> statements in Smarty have much the same flexibility as PHP
 | |
|   <ulink url="&url.php-manual;if">if</ulink>
 | |
|   statements, with a few added features for the template engine.
 | |
|   Every <varname>{if}</varname> must be paired with a matching
 | |
|   <varname>{/if}</varname>. <varname>{else}</varname> and
 | |
|   <varname>{elseif}</varname> are also permitted. All PHP conditionals
 | |
|   and functions
 | |
|   are recognized, such as <emphasis>||</emphasis>, <emphasis>or</emphasis>,
 | |
|   <emphasis>&&</emphasis>, <emphasis>and</emphasis>,
 | |
|   <emphasis>is_array()</emphasis>, etc.
 | |
|  </para>
 | |
|  <para>
 | |
|   If <link linkend="variable.security"><parameter>$security</parameter></link> is enabled
 | |
|   then <emphasis>IF_FUNCS</emphasis> array in the <link
 | |
|   linkend="variable.security.settings"><parameter>$security_settings</parameter></link> array.
 | |
|  </para>
 | |
|  <para>
 | |
|   The following is a list of recognized qualifiers, which must be
 | |
|   separated from surrounding elements by spaces. Note that items listed
 | |
|   in [brackets] are optional. PHP equivalents are shown where applicable.
 | |
|  </para>
 | |
| 
 | |
|  <informaltable frame="all">
 | |
|   <tgroup cols="4">
 | |
|    <colspec colname="qualifier" align="center" />
 | |
|    <colspec colname="alternates" align="center" />
 | |
|    <colspec colname="meaning" />
 | |
|    <colspec colname="example" />
 | |
|    <colspec colname="php" />
 | |
|    <thead>
 | |
|     <row>
 | |
|      <entry>Qualifier</entry>
 | |
|      <entry>Alternates</entry>
 | |
|      <entry>Syntax Example</entry>
 | |
|      <entry>Meaning</entry>
 | |
|      <entry>PHP Equivalent</entry>
 | |
|     </row>
 | |
|    </thead>
 | |
|    <tbody>
 | |
|     <row>
 | |
|      <entry>==</entry>
 | |
|      <entry>eq</entry>
 | |
|      <entry>$a eq $b</entry>
 | |
|      <entry>equals</entry>
 | |
|      <entry>==</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry>!=</entry>
 | |
|      <entry>ne, neq</entry>
 | |
|      <entry>$a neq $b</entry>
 | |
|      <entry>not equals</entry>
 | |
|      <entry>!=</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry>></entry>
 | |
|      <entry>gt</entry>
 | |
|      <entry>$a gt $b</entry>
 | |
|      <entry>greater than</entry>
 | |
|      <entry>></entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry><</entry>
 | |
|      <entry>lt</entry>
 | |
|      <entry>$a lt $b</entry>
 | |
|      <entry>less than</entry>
 | |
|      <entry><</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry>>=</entry>
 | |
|      <entry>gte, ge</entry>
 | |
|      <entry>$a ge $b</entry>
 | |
|      <entry>greater than or equal</entry>
 | |
|      <entry>>=</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry><=</entry>
 | |
|      <entry>lte, le</entry>
 | |
|      <entry>$a le $b</entry>
 | |
|      <entry>less than or equal</entry>
 | |
|      <entry><=</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry>===</entry>
 | |
|      <entry></entry>
 | |
|      <entry>$a === 0</entry>
 | |
|      <entry>check for identity</entry>
 | |
|      <entry>===</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry>!</entry>
 | |
|      <entry>not</entry>
 | |
|      <entry>not $a</entry>
 | |
|      <entry>negation (unary)</entry>
 | |
|      <entry>!</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry>%</entry>
 | |
|      <entry>mod</entry>
 | |
|      <entry>$a mod $b</entry>
 | |
|      <entry>modulous</entry>
 | |
|      <entry>%</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry>is [not] div by</entry>
 | |
|      <entry></entry>
 | |
|      <entry>$a is not div by 4</entry>
 | |
|      <entry>divisible by</entry>
 | |
|      <entry>$a % $b == 0</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry>is [not] even</entry>
 | |
|      <entry></entry>
 | |
|      <entry>$a is not even</entry>
 | |
|      <entry>[not] an even number (unary)</entry>
 | |
|      <entry>$a % 2 == 0</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry>is [not] even by</entry>
 | |
|      <entry></entry>
 | |
|      <entry>$a is not even by $b</entry>
 | |
|      <entry>grouping level [not] even</entry>
 | |
|      <entry>($a / $b) % 2 == 0</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry>is [not] odd</entry>
 | |
|      <entry></entry>
 | |
|      <entry>$a is not odd</entry>
 | |
|      <entry>[not] an odd number (unary)</entry>
 | |
|      <entry>$a % 2 != 0</entry>
 | |
|     </row>
 | |
|     <row>
 | |
|      <entry>is [not] odd by</entry>
 | |
|      <entry></entry>
 | |
|      <entry>$a is not odd by $b</entry>
 | |
|      <entry>[not] an odd grouping</entry>
 | |
|      <entry>($a / $b) % 2 != 0</entry>
 | |
|     </row>
 | |
|    </tbody>
 | |
|   </tgroup>
 | |
|  </informaltable>
 | |
|  <example>
 | |
|   <title>{if} statements</title>
 | |
|   <programlisting>
 | |
| <![CDATA[
 | |
| {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}
 | |
| ]]>
 | |
|   </programlisting>
 | |
|  </example>
 | |
| 
 | |
| 
 | |
|  <example>
 | |
|    <title>{if} with more examples</title>
 | |
| <programlisting>
 | |
|  <![CDATA[
 | |
| {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}
 | |
|   ]]>
 | |
| </programlisting>
 | |
|  </example>
 | |
| 
 | |
| </sect1>
 | |
| <!-- Keep this comment at the end of the file
 | |
|      Local variables:
 | |
|      mode: sgml
 | |
|      sgml-omittag:t
 | |
|      sgml-shorttag:t
 | |
|      sgml-minimize-attributes:nil
 | |
|      sgml-always-quote-attributes:t
 | |
|      sgml-indent-step:1
 | |
|      sgml-indent-data:t
 | |
|      indent-tabs-mode:nil
 | |
|      sgml-parent-document:nil
 | |
|      sgml-default-dtd-file:"../../../../manual.ced"
 | |
|      sgml-exposed-tags:nil
 | |
|      sgml-local-catalogs:nil
 | |
|      sgml-local-ecat-files:nil
 | |
|      End:
 | |
|      vim600: syn=xml fen fdm=syntax fdl=2 si
 | |
|      vim: et tw=78 syn=sgml
 | |
|      vi: ts=1 sw=1
 | |
| --> |