Control flow

The control flow tag creates conditions that decide whether blocks of code get executed.

If

The if statement checks whether a condition is true or false. If the condition is true, the code within the if statement gets executed.

πŸ“˜

Let's assume that the variable profit contains the value 999,99.

{% if profit > 0 %}
  There's a profit this year.
{% endif %}
Output
There's a profit this year.

Else

The else statement is used in combination with the if statement. If the if statement is false, the else part will be executed.

πŸ“˜

Let's assume that the variable profit contains the value -100,00.

{% if profit > 0 %}
  There's a profit this year.
{% else %}
  There's a loss this year.
{% endif %}
Output
There's a loss this year.

Elsif

The elsif statement is used in combination with the if statement. If the if statement is false, there will be checked if the elsif statement is true.

πŸ“˜

Let's assume that the variable profit contains the value -100,00.

{% if profit > 0 %}
  There's a profit this year.
{% elsif profit < 0 %}
  There's a loss this year.
{% else %}
  There's nor a profit, nor a loss this year.
{% endif %}
Output
There's a loss this year.

Ifi

An ifi statement can be seen as an if statement that is always true in input view and depends on the condition in export view. The ifi statement can be combined with the elsif and/or else tags.

πŸ“˜

In Silverfin there's an input view with fields that can be modified and an export view where only text is shown.

πŸ“˜

Assume that the variable supply_change is 0.

{% ifi supply_change != 0 %}
  The supplies changed with {{ supply_change }}
{% endifi %}
Output (in input view)
The supplies changed with 0.
Output (in export view)

Unless

Code within unless tag will be executed unless the statement is true. It is the antagonist of the if statement. The unless statement can be combined with elsif and/or else tags.

πŸ“˜

Let's assume that the variable profit contains the value -999,99.

{% unless profit > 0 %}
  There is a loss
{% endunless %}
Output
There is a loss

Case

The case statement is similar to a series of if statements on the same expression. In many occasions, you may want to compare the same variable with different variables.

πŸ“˜

Let's assume that the fiscal_year variable is 2019.

{% case fiscal_year %}
{% when 2018 %}
  {% assign ratio = 0.23 %}
{% when 2019 %}
  {% assign ratio = 0.27 %}
{% else %}
  {% assign ratio = 0.19 %}
{% endcase %}


{{ ratio }}
Output
0.27

❗️

Blank vs zero

Sometimes you will need to check if a certain value equals zero. This is different from validating if a certain value is blank.

If you also want to check for blank, that condition needs to be added explicitly by adding '== blank'

The if-statement will always be false in the following situation where the variable is blank:

{% assign test_variable =  %}
{% if test_variable == 0 %}
  The value equals zero.
{% else %}
  The value is different from zero, including blank.
{% endif %}
Output
The value is different from zero, including blank.

Instead of adding "== blank" to a condition that checks for zeroes, you can use 'INT()' on the variable you want to check. Doing so will convert blank variables into zeroes, which makes it no longer necessary to combine a check on zero and blank as that variable can never be blank anymore.

{% if INT(test_variable) == 0 %}
  The value equals zero.
{% else %}
  The value is different from zero, excluding blank.
{% endif %}
Output
The value equals zero.