If / Else If / Else

The if flow evaluates the given expression and executes the body if the expression resulted in true. Otherwise, the body is ignored. It can be chained with else if and else, stopping the execution after the first expression resulting in true.

If must always start the condition chain and can be optionally followed by else if and else. Else has to always be the last, and can only be used in the chain.

Syntax

Basic syntax of if tag is as follows:

{[ if [statement] ]}
   Content that gets executed when statement evaluates to true
{[/]}

When additional conditions are needed, use else if and / or else to chain them:

{[ if [statement] ]}
   Content that gets executed when first statement evaluates to true
{[ elseif [statement] ]}
   Content that gets executed when first statement evaluates to false and second to true
{[ else ]}
   Content that gets executed when everything above evaluates to false (fallback)
{[/]}

Note that else doesn't have statement as it serves as a fallback when everything else fails.

Evaluating the statement

This section is a work in progress. If definition is not yet complete

if or else if evaluates to true when one of the following conditions is true:

  • statement resulting type is Boolean and == true

  • statement resulting type is Int and >= 1

  • statement resulting type is String and the string is not empty

Example use case

If can be used to render structurally different data based on the input values. In this example, a different user menu gets rendered based on whether the user is admin, team member, or a visitor.

Data

{
    "user": {
        "name": "Jiri",
        "type": "member"
    }
}

Blueprint

<ul>
    {[ if user.type.equals("admin") ]}
    <li>Manage Database</li>
    <li>Manage Users</li>
    <li>Manage Team</li>
    {[ elseif self.user.type.equals("member") ]}
    <li>Manage Team</li>
    {[ else ]}
    <li>Request Membership</li>
    {[/]}
    <li>Logout</li>
</ul>

Result

<ul>
    <li>Manage Team</li>
    <li>Logout</li>
</ul>

Finally, if we change user.type to admin, the output will look like the following:

<ul>
    <li>Manage Database</li>
    <li>Manage Users</li>
    <li>Manage Team</li>
    <li>Logout</li>
</ul>

Last updated