0

I’m looking to find if the title of the current collection page exists inside the array of shop.vendors and display a div only if the collection page title does not exist in the array. From the documentation it seems like I should be able to do this but it doesn't work?

{%- unless shop.vendors contains collection.title -%}
 <div> display content in here if condition is not met </div>
{%- endunless -%}
  • That is correct, try to debug the values of the two variables in the condition like `{{shop.vendors}}` and `{{collection.title}}` – Fabio Filippi Jul 06 '22 at 09:47
  • @FabioFilippi Just tried to debug and the output was as follows `
    Fairfax & FavourHancock ScotlandHolland CooperOliami Test
    Fairfax & Favour
    ` The first div is shop.vendors and the second div is collection.title, so it seems like each object is correct but the unless statement still isn't working?
    – David Ferguson Jul 06 '22 at 10:33
  • your code is correct, but I think you misread about unless. it show when it false. but it in your case it is true, because collection title exits into shop.vendors – Onkar Jul 06 '22 at 11:28
  • Are you sure? In the docs it says show this element unless condition is met? – David Ferguson Jul 06 '22 at 11:59
  • For example I have tried to brute force it by doing `unless shop.vendors contains "Fairfax & Favour"` which worked as I expected, but I need it to work for every page – David Ferguson Jul 06 '22 at 12:00
  • It work because there is difference between & and & – Onkar Jul 06 '22 at 12:02
  • Take a look here for more information : https://stackoverflow.com/questions/30822160/does-liquid-have-a-does-not-contain-or-not-in-array-operator – Onkar Jul 06 '22 at 12:02
  • unless works only when the condition is falsy, and in your case it become true. – Onkar Jul 06 '22 at 12:03

2 Answers2

1

So I've found out why this wasn't working, for anyone else who comes across this

shop.vendors renders an ampersand as &amp rather than &, while collection.title renders it as &. Therefore, if you try to compare the names it won't work.

The solution is doing | escape to only shop.vendors so they're formatted the same, then the comparison will work. I also did | remove to get rid of the ampersand entirely as it's not strictly needed in the name comparison in my case.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
-1

You can rewrite your code as:

{% for vendor in shop.vendors %}
  {% if vendor == collection.title %}
     <div> display content in here if condition is not met </div>    
  {% endif %}
{% endfor %}
  • I'm not looking to have multiple iterations of the same object though – David Ferguson Jul 06 '22 at 11:56
  • Ok then it should work: **`{% if shop.vendors contains collection.title %}
    display content in here if condition is not met
    {% endif %}`**
    – Mandasa Technologies Jul 06 '22 at 12:03
  • @MandasaTechnologies, want to show the div with content when title is not into vendors list, okay? – Onkar Jul 06 '22 at 12:06
  • **`{% if shop.vendors contains collection.title %} {% else %}
    display content in here if condition is not met
    {% endif %}`** Use this.
    – Mandasa Technologies Jul 06 '22 at 12:10
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 13 '22 at 01:31