2.7.4. Conditions

OPT supports conditional instruction if ... elseif ... else ... /if known from programming languages. It allows to show the parts of the template, where specified condition is true. A condidion is a correct OPT expression that should return true or false.

Example 2.48. Conditional instruction

{if $access eq 1}
{* this will be displayed, if $access is equal 1 *}
<p>Welcome, administrator</p>
{elseif $access eq 2}
{* this will be displayed, if the previous condition is false *}
{* and $access is equal 2 *}
<p>Welcome, editor</p>
{else}
{* This will be displayed, if all the conditions are false *}
<p>Permission denied</p>
{/if}

Notes:

  1. elseif and else are optional.
  2. You can specify as many elseif nodes as you need.
  3. if and elseif use their own parameter format by default. You may force them to use the standard one by setting one of these configuration directives to true: strictSyntax, xmlsyntaxMode. The parameter is then called test. The following code shows the if instruction in strict syntax mode:

    Example 2.49. Conditions in XML syntax mode

    {* Position syntax *}
    {if=$access eq 1}
    <p>Welcome, administrator</p>
    {* XML style syntax *}
    {elseif test="$access eq 2"}
    <p>Welcome, editor</p>
    {/if}