If, else, elsif

Video outline

Using control flow, it is possible to conditionally render certain pieces of your code, which means, to only use this code if a certain condition is met.

In this video we will cover the if, elsif and else statements which are used the most,

If-statements

An if statement is used to check whether a certain condition has been met.

Any code or content which has been placed inside the if statement will ONLY be shown/executed in the template IF that condition is true.

A standardized if-statement can be created by entering ‘if’ followed by ‘tab’.

{% if condition %}content{% endif %}

An if statement consists of three parts, which are the opening tag including the condition; the content, which is the code or text to be executed in case the condition is met; and the closing tag. Without a closing tag the if-statement will not work.

The condition, which is in the opening tag, also consists of three parts, which are the main variable, the operator, and the variable you are comparing with. Both variables can usually be replaced by numbers.

Here you see two examples of conditions:

company_name == "Silverfin"

my_age > 35

In this code example, we will print some text in case a variable is greater than 0.

{% assign total_result = 1000 %}

{% if total_result > 0 %}
  profit
{% endif %}

Elsif-statement

An elsif statement is always created in combination and within an if statement. It is used when you want to check other conditions in case the condition in your if statement is false. When your if-statement is true, the condition in the elsif statement is never met, the elsif statement won’t even be rendered.

Here you see an example in which al elsif statement is added to the previous example.

{% assign total_result = -1000 %}

{% if total_result > 0 %}
  profit
{% elsif total_result < 0 %}
  loss
{% endif %}

Note that you could add multiple elsif statements if you wish, which creates different scenarios. Note that fulfilling one condition in this structure, avoids rendering all statements after it.

Else statement

An else statement is always created in combination with an if, unless or case statement and is always the last statement within the structure.

It is used when you want to execute certain code when all other conditions in your if statement is false.

Using an else-statement, you can cover all scenarios not covered by a condition coded specifically in any of the statements above it.

Here you see an example of an else statement, which in this example covers the scenario when the variable equals zero.

{% assign total_result = -1000 %}

{% if total_result > 0 %}
  profit
{% elsif total_result < 0 %}
  loss
{% else %}
  Break-even
{% endif %}

Conclusion

Control flow lets you render specific pieces of code or text conditionally.

  • {% if %} statement: used to only execute code when a certain condition is fulfilled
  • {% elsif %} statement: used to check another condition in case the condition in the if statement is not fulfilled. Multiple elsif statements can be used to check multiple conditions
  • {% else %} statement: used to print execute pre-set code in case all conditions in both if and elsif statements have not been fulfilled