Ad

Breaking Else If Statement

- 1 answer

I am using Shopify's Liquid language to create some lists of content for my store, but I am having trouble getting the logic right with this if statement that I am using.

I am trying to assign classes to a li html element based on a few conditions so that I can eventually sort these into groups of 5 links later.

Firstly, I am checking that the linklist is less than or equal to 5 links if not, go on to the next one, are all the links equal to 10 or more, if not, go to the next, are the links equal to 15 etc etc.

I know my logic is not quite right somewhere, my code seems to end at the second elsif, even when my linklist has 20 or so links in them.

{% if linklists[footer_linklist-2].links.size <= 5 and linklists[footer_linklist].links == empty %}

   columns_1

{% elsif linklists[footer_linklist-2].links.size >= 10 and linklists[footer_linklist].links == empty %}

   columns_2

{% elsif linklists[footer_linklist-2].links.size >= 15 and linklists[footer_linklist].links == empty %}

   columns_3

{% endif %}
Ad

Answer

You need to order your elsif in descending order. The >=10 in the first one will catch everything 10 or more (including 15, 20 etc.). Try this:

{% if linklists[footer_linklist-2].links.size <= 5 and linklists[footer_linklist].links == empty %}

   columns_1

{% elsif linklists[footer_linklist-2].links.size >= 15 and linklists[footer_linklist].links == empty %}

   columns_3

{% elsif linklists[footer_linklist-2].links.size >= 10 and linklists[footer_linklist].links == empty %}

   columns_2

{% endif %}
Ad
source: stackoverflow.com
Ad