If/Else Blocks

With an if/else block, you can use one part of your template in one situation and a different part in a different situation. For example, if you have a template that outputs a list of news articles, you may want to show a "no messages found" type message if there are no news articles to output. In this case you would test for the number of articles being zero.

Example A.4. Using an if/else block to determine what to output.

{if $numberOfArticles == 0}
                
    No articles found.
    
{else}

    Output the articles here!
    
{/if}

You can also use elseif to test for multiple conditions. The following figure demonstrates how this may be useful. This code determines whether or not to output the word "articles" as a plural.

Example A.5. Using an if/elseif/else block to determine what to output.

{if $numberOfArticles == 0}

    No articles found.
    
{elseif $numberOfArticles == 1}

    1 article found.
    
{else}

    {$numberOfArticles} articles found.

{/if}

As an aside, you could also achieve the same output as the previous listing with the following code. Rather than using elseif, it uses multiple if statements.

Note

The else part of an if/else block is optional.

Example A.6. Nested if/else blocks.

{if $numberOfArticles == 0}

    No articles found.
    
{else}

    {$numberOfArticles} article{if $numberOfArticles != 1}s{/if} found.

{/if}