I have some doubt about how to combine “or” and “and” in an if statement.
If I have either one of two conditions to be met (let’s say Or1 and Or2) and I have 1 condition that should always be met (let’s say And1), and I like to combine these in an if statement, how should I do so?
{% if Or1 or Or2 and And1 %} nor {% if Or1 or And1 or Or2 and And1 %} appear to work correctly, what would be the right way please?
Using a combination of or and and statements is tricky in liquid. There are 2 things about this issue you need to know.
You can’t use brackets ( ) to prioritize . It just won’t work when using logic.
The logic works from right to the left.
This means that if you for example have following statement: {% if Or1 or Or2 and And1 %}. Liquid will first check Or2 and And1 and then check the last or statement.
Knowing this doesn’t solve all problems though. The best way to address this issue is by using multiple if statements. Let me give you an example to explain this.