Skip to content

Conditionals

Conditionals enable you to control the logical flow within your templates by performing various checks and operations. They are fundamental for determining which parts of a template are rendered based on specific conditions. This section provides a comprehensive overview of all available conditional statements and clear examples to demonstrate their practical use.

Available Functionality

  • if: Checks if a condition is true.
  • elsif: Checks another condition if the previous if was false.
  • else: Provides a fallback if none of the previous conditions were met.
  • unless: Checks if a condition is false.
  • case/when: Selects among multiple conditions, similar to a switch statement in other programming languages.

If

Example

{% if user %}
  Hello, {{ user.name }}!
{% endif %}

Output when user is present

Hello, John!

Output when user is absent

If-Else Conditional

Input:

{% if user %}
  Hello, {{ user.name }}!
{% else %}
  No user logged in.
{% endif %}

Output when user is present

Hello, John!

Output when user is absent

No user logged in.

If-Elsif-Else

Checks if a condition is true.

Example

{% if user %}
  Hello, {{ user.name }}!
{% elsif guest %}
  Hello, Guest!
{% else %}
  No user or guest logged in.
{% endif %}

Output when user is present

Hello, John!

Output when only guest is present

Hello, Guest!

Output when neither user nor guest is present

No user or guest logged in.

Unless

Example

{% unless user %}
  No user logged in.
{% endunless %}

Output when user is present

Output when user is absent

No user logged in.

Case/When

Example {% assign favorite_color = 'blue' %} {% case favorite_color %} {% when 'red' %} Your favorite color is red. {% when 'blue' %} Your favorite color is blue. {% else %} You have a different favorite color.

Output

Your favorite color is blue.

Nested Conditionals

Conditionals can be nested to create more complex logic:

Example

{% if user %}
  {% if user.admin %}
    Welcome, Admin {{ user.name }}!
  {% else %}
    Welcome, User {{ user.name }}!
  {% endif %}
{% else %}
  No user logged in.
{% endif %}

Output when user is admin

Welcome, Admin John!

Output when user is not admin

Welcome, User John!

Output when no user is logged in

No user logged in.

Logical Operators

These operators enable you to evaluate multiple expressions simultaneously, returning a true or false result based on the combination of conditions.

Input:

{% if user and user.admin %}
  Welcome, Admin {{ user.name }}!
{% elsif user %}
  Welcome, User {{ user.name }}!
{% else %}
  No user logged in.
{% endif %}

Output when user is admin

Welcome, Admin John!

Output when user is not admin

Welcome, User John!

Output when no user is logged in

No user logged in.

Comparing Values

You can compare variables and values within:

Input

{% assign age = 25 %}
{% if age > 18 %}
  You are an adult.
{% else %}
  You are a minor.
{% endif %}

Output

You are an adult.

Key Points

  • if, elsif, else: Fundamental conditionals for controlling the flow based on true or false conditions.
  • unless: Executes code if the condition is false.
  • case/when: Handles multiple conditions more succinctly compared to multiple if statements.
  • Nested Conditionals: For more complex logic by nesting conditions.
  • Logical Operators: Combine multiple conditions using and, or, etc.
  • Value Comparison: Compare values within conditionals to drive logic based on data.