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 %}
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 %}
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 %}
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 %}
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 %}
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 }}
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 %}
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 %}
Updated almost 2 years ago