LIQUID FOR TECHNICAL MARKETERS: 7 Rules of Liquid Conditionals

Previously, we posted a video called “5 Rules of Liquid Conditionals”.

We are back with 2additional rules that will help you conquer Liquid Conditionals!

Follow these rules, and you will never have problems when working withLiquid Conditionals.

🗣️ WATCH FULL YOUTUBE VIDEO HERE 🗣

https://youtu.be/O6FhoXz55ps

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 the 7 Rules of Liquid Conditionals which will significantly help you understand and use Liquid Conditionals successfully.

You know the rules, and so do I.

Let’s get started.

Rule #1: EVERY {% if %} statement NEEDS a matching
{% endif %} statement

We put this one first, because it’s a very common syntax issue we see: Marketers forget to close the Liquid Conditional if statement with the endif statement.

One very helpful strategy for whenever you’re writing Liquid from scratch is if you end up writing an if statement, just add the endif statement right away.

That way, you start to build the habit that every if statement needs a matching endif statement.

You know when you’re working with HTML, and the smart editors always automatically throw in the closing HTML tags whenever you write an opening tag? It’s the same idea.

Without an endif statement, Braze Liquid will think you never finished your Conditional and will think the rest of your code is part of the Liquid, or often times just not render at all.

One final note to make on this rule: this applies for even nested if statements too. Nested if statements are when you have an if statement, inside another if or elif or else statement. The same rule applies; every if statement needs a matching endif statement! Here’s a quick example below.

{% if weather == “rainy” %}

{% if precipitation == “hail” %}

{% assign message = “Stay inside!” %}

{% endif %}

{% assign message = “Bring an umbrella!” %}

{% endif %}

Notice how the first endif closes out the inner if statement, and the last endif closes out the outer if statement.

Rule #2: {% if %} is REQUIRED for every conditional block, but {% elsif %} and {% else %} are NOT required!

A Conditional block only requires an if and a matching endif statement to function properly.

elsif and else statements might be necessary to achieve your logic, but they’re not required for every Conditional block.

If you’re just starting out with learning Braze Liquid, practice writing Conditional Blocks that just have if and endif statements to become more familiar. One step at a time!

Rule #3: In conditional statements, use DOUBLE “==”

Inside your Conditional statements, whenever you want to check whether a variable’s value matches something, we use double equal signs, not a single equal sign.

For example, if holiday == “Christmas”, not holiday = “Christmas”.

A single variable is only used when we’re assigning variables.

Rule #4: Else statements DO NOT require a logic statement

else is your default, fallback logic. It captures every other case besides what you listed in your if and elsif statements above. That’s also why else statements come at the very end of every Conditional block, although of course, above the endif statement.

Else statements are simple; just leave it blank because it doesn’t require a logic statement!

Rule #5: When using Logical Operators to include multiple Conditional statements, be sure to write out the full Conditional statement each time

Let’s take a look at this Conditional block below:

{% assign status = “gold” %}

{% if status == “gold” or “silver” %}

Thank you for being a gold/silver member!

{% endif %}

This Conditional block will throw an error. Why?

Intuitively, our if-statement makes sense. If the variable “status” is equal to “gold” or “silver”, we render the thank you message.

However, Liquid separately evaluates each Conditional statement that’s separated by the Logical Operator, the “or” in our example. So in our case, the two statements that our if-statement is evaluating are:

  1. status == “gold”, which is true, in our case, and
  2. “silver”

What does it mean to say “if silver”? If what is silver? Notice that we need to add the full Conditional statement for Liquid to understand our logic.

Another way to think about this mistake is to consider switching around the two Conditional statements:

{% assign status = “gold” %}

{% if “silver” or status == “gold” %}

Thank you for being a gold/silver member!

{% endif %}

At least in the example with two Conditional statements, we should be able to change the order between the two with no problem, but we see that the mistake is a bit more obvious written this way.

So finally, here’s the correct way to write this example, with both Conditional statements fully written out:

{% assign status = “gold” %}

{% if status == “gold” or status == “silver” %}

Thank you for being a gold/silver member!

{% endif %}

Now, if our variable status equals either gold or silver, the thank you message will render properly.

Rule #6: When using Conditional Statements:
Liquid will read from TOP to BOTTOM, and only the FIRST, TRUE statement between each
{% if %} and {% endif %} will render

This rule could’ve been broken up into 2 rules because there are two ideas here.

First, as with most cases in Computer Science, Braze Liquid reads from top to bottom. That means that every logic statement will be evaluated starting from your very first if statement, then moving down to the next statements, ending with your else statement.

Second, as Liquid is evaluating your logic statements from top to bottom, Liquid will only render the FIRST, TRUE statement.

So as soon as Liquid detects a logic statement that is correct, that statement will be run, and no other logic statements below will be considered.

In other words, every Conditional Block only renders 1 statement.

And now, we have our last rule:

Rule #7: Do not create overlapping Conditional statements among the Conditional branches

Here’s an example to show what errors can happen if we create overlapping Conditional statements. By the way, this example relies on Rule 6, so think carefully.

{% assign points = 25 %}

{% if points > 10 %}

You have more than 10 points! You’re doing great!

{% elsif points > 20 %}

More than 20 points?! You’re a rockstar!

{% endif %}

Rule #6 states: Liquid will read from TOP to BOTTOM, and only the FIRST, TRUE statement between each {% if %} and {% endif %} will render.

Since we have 25 points, it does make our first if-statement true, which is that points is greater than 10.

However, with 25 points, it’s probably intended and more satisfying to receive the second message for over 20 points.

There’s two ways to fix this issue:

1. You could swap the two statements, so that points > 20 comes before points > 10. That way, you’re at least prioritizing the higher points message and making your way down the lower points messages. Even though this solution still doesn’t follow Rule #7, it works in our situation.

2. The preferred solution is to add clear boundaries for each Conditional statement using Logical Operators. Take a look at the solution below:

{% assign points = 25 %}

{% if points > 10 and points <= 20 %}

You have more than 10 points! You’re doing great!

{% elsif points > 20 %}

More than 20 points?! You’re a rockstar!

{% endif %}

This way, regardless of how many points our user has, they’ll only fall under exactly one Conditional branch.

Thank You!

That’s it for today! Study and follow these 7 Rules for Liquid Conditionals, and you will become a Conditional Expert in no time.

Thank you for watching, and see you next time!

allan@fornowmarketing.com

fornowmarketing.com

Previous
Previous

BRAZE LIQUID — Data Types — Liquid For Technical Marketers

Next
Next

BRAZE LIQUID — 3 Types of Liquid Code — Liquid For Technical Marketers