Skip to main content

Control Flow with Conditional and Logical Operators

Like most other programming languages, Scriptlets support if and else statements to only output content when certain conditions are met.

if elsif

{% assign name = "{{ Your Full Name }}" %}
{% if name == "Ronald" %}
Hello {? name ?}
{% elsif name == "Sofia" %}
Hola {? name ?}
{% else %}
Hi {? name ?}
{% endif %}

if else (OR)

{% assign name = "{{country name}}" %}
{% if name == "Holland" or name == "Netherlands" %}
The flag of {{country name}} is 🇳🇱
{% endif %}

if else (AND)

{% assign name = "{{Country Name}}" %}
{% assign lowername = name | downcase %}
{% assign uppername = name | upcase %}
{% if lowername == "india" and uppername == "INDIA" %}
We're open in {{ Country Name }}
{% else %}
Sorry, we are not in {{ Country Name }} yet!
{% endif %}

if else (Number)

{% assign number = "{{Student Age}}" %}
{% if number > 18 or number == 18 %}
Appliant is an adult!
{% else %}
Applicant is {? number ?} years old!
{% endif %}

switch case

Use the switch statement to compare a variable with different values

{% assign name = "Alejandro" %}
{% case name %}
{% when "Alejandro" %}
Hola {? name ?}!
{% when "Oliver", "Arthur", "Freddie" %}
Hello {? name ?}!
{% else %}
Do I know you {? name ?}?
{% endcase %}

unless

This is the exact opposite of the if statement. It will only render the block if the condition is false.

{% unless {{ Country Name }} == "USA" %}
Applications from {? {{Country Name}} | capitalize ?} are not accepted.
{% endunless %}

capture

Captures the string inside of the opening and closing tags and assigns it to a variable. Variables created through capture are strings.

{% capture var_name %}
how are you
{% endcapture %}
{? var_name | strip | capitalize | append: "?" ?}

// Output: How are you?

Capture a complex string contain a variable name

{% assign name = "Angus" %}
{% capture greeting %}how are you {? name ?}
{% endcapture %}
{? greeting | strip | capitalize | append: "?" ?}

// Output: How are you Angus?