Liquid Code Query

I have a button that I want to display based on a products custom field called ‘Estate’ that is setup as a checkbox selection -

{% if this['Estate'] == 'Camyr Allyn Vacy Acres' %}	
        <a data-toggle="modal" data-target="#newsletterModalcamyr" class="btn btn-primarybl">Enquire Now</a>
        {% endif %}
          {% if this['Estate'] == 'Spring Farm Vulcan Ridge Nepean Village' %}	
        <a data-toggle="modal" data-target="#newsletterModalspringfarm" class="btn btn-primarybl">Enquire Now</a>
        {% endif %}
        {% if this['Estate'] == 'Seascape' %}	
        <a data-toggle="modal" data-target="#newsletterModalseascape" class="btn btn-primarybl">Enquire Now</a>
        {% endif %}
        {% if this['Estate'] == 'Cornish Group' %}	
        <a data-toggle="modal" data-target="#newsletterModalcornish" class="btn btn-primarybl">Enquire Now</a>
        {% endif %}

I can’t seem to get this to display though when I select one of the options.

Hi @SiroccoDigital
This is because a property of type checkbox will be output (in Liquid) as an array. So you need to compare each possible value individually.

Probably the simplest way in this case would be like this (otherwise you’d need to loop through the array and check for exact values):

{% if this['Estate'] contains 'Camyr' or this['Estate'] contains 'Allyn' or this['Estate'] contains 'Vacy' or this['Estate'] contains 'Acres'%}
<!-- show relevant button -->
{% endif %}
... and so on.

This is assuming that Camyr Allyn Vacy Acres are all separate values, otherwise, just combine these in the code above where necessary.

NOTE: using the contains comparison can have issues if one of your single values is also part of another value. For example, if one value was just Village and another value was Nepean Village, then both would return true for contains 'village'. But it looks like it should be ok in your case.

Also, with multiple IF statements like this, it could be possible to return true for multiple cases. For example, if an item had Seascape and Cornish Group checked, you’d get 2 buttons showing.
One way to resolve this could be to use elsif conditions within one IF statement so that only one condition wins out. But you’d need to consider which value/s have priority in this case.

@Adam.Wilson this works perfectly! Thank you so much for the detailed feedack. This has really given me a lot of options.

1 Like

contain has saved me in so many liquid situations. Most underrated operator :wink: