LIQUID FOR TECHNICAL MARKETERS: Conditionals — if, elsif, else

LIQUID FOR TECHNICAL MARKETERS: Conditionals — if, elsif, else

🗣️ WATCH FULL YOUTUBE VIDEO HERE 🗣

https://youtu.be/oyp53kJf1xA

What’s up everyone, my name is Allan, Founder and Consultant at For Now Marketing, and welcome to LIQUID FOR TECHNICAL MARKETERS!

If your goal is to improve your Liquid coding skills, then please

  1. like this video
  2. subscribe to our channel
  3. watch this video all the way to the end
  4. and keep an eye out for more videos in the future.

Today, we’re going to talk about Liquid Conditionals. This is a very exciting topic because it’s one of the most powerful features of Liquid.

If you haven’t already, please watch the video on Comparison Operators! You can find it here or in the Youtube description below.

And here’s a meme to get us started: “you won’t forget conditionals, if you practice them regularly”. Certainly the case with Conditionals, and also the case with really everything else in Liquid. So let’s get started!

Liquid Conditionals — “if” & Syntax Breakdown

A Liquid Conditional is a block of code that only renders if the condition is true. That’s why they’re called Liquid Conditionals; the code renders on the condition that the logic statement is true.

So let’s start with the most basic example: if-statements. And let’s start by breaking down the anatomy of an if-statement into the individual syntaxes.

Liquid Conditional is a type of Liquid Tags, meaning they’ll be surrounded by {% %}. So we start this if-statement with “curly bracket percentage”, followed by the word “if”.

Next comes our conditional statement. This is where those comparison operators from the previous video comes in handy. We need those comparison operators to determine whether a statement is “true” or “false”.

For example, is 7 >= 2? If the conditional statement is true, then the block of code under this if-statement gets rendered. If the conditional statement is false, then the block of code does not get rendered.

And lastly, the conditional statement is followed by the ending Liquid tag: percent curly bracket.

I’m sure you also noticed the endif-statement. Every Liquid Conditional ends with the endif-statement, which starts with curly bracket percentage, the word endif in one word, and percentage curly bracket.

elsif

Well, what happens if our conditional statement is false? Then what? That’s it?

What if we wanted to add additional conditional statements, to have a different message render depending on the conditional statement?

For example, we have a specific message for someone named Allan, and we have another message for someone not named Allan!

That’s when the “elsif” tag comes into play. “elsif” is the words “else if” mashed together.

“elsif” is saying “if the first or previous conditional statement is false, then, try this next conditional statement instead”.

So “elsif” is how we’re able to add multiple conditional blocks, and there’s no limit to how many “elsif” statements we can add.

So here’s an example of an elsif statement that’s going to run if name is not equal to Allan. Let’s change our name variable to Bill. Then the first if-statement isn’t going to run, so we move on to the next elsif statement.

Since the next elsif-statement is true, we should see this message get rendered.

else

Lastly, we have what’s called the “else” tag. “else” is the last-resort, catch-all block that will render, if all the “if” blocks and “elsif” blocks are false.

And because it is the last-resort, catch-all block, the “else” tag will always go at the bottom.

Here’s an example: let’s say we had a special message for someone named Allan, Bill, and Jon. But that’s all the messages we had prepared. So if your name is anything besides those 3, you’d get a more general message without a specific mention of your name.

This is when the “else” block will have its shining moment and finally get to render the code inside the “else”. Let’s take a look at an example with the name “Spencer”.

And there we have it, the “else” block ran, because there was no “if” or “elsif” statements that was checking for the name “Spencer”.

Thank You!

That’s it for today! Like we saw at the beginning of the video, “you won’t forget conditionals, if you practice them regularly”. So be sure to practice by writing some new example Liquid Conditionals on your own.

There will be another videos to help further clarify your understanding of Liquid Conditionals, so keep an eye out for that video.

Thank you for watching, and see you next time!

allan@fornowmarketing.com

fornowmarketing.com

Previous
Previous

LIQUID FOR TECHNICAL MARKETERS: Date Filter & strftime

Next
Next

LIQUID FOR TECHNICAL MARKETERS: Comparison Operators