Not sure why NOT EQUAL or EQUAL not working.

Not sure why NOT EQUAL or EQUAL not working.

CoreyNg
Shopify Partner
41 4 1

Thanks in advance.

Object: shop.vendors

vendors: AAA, AAB, ABC, BBB, CCC

{% assign temp_vendor = "" %}

{% assign add_anchor = false %}

{% for vendor in shop.vendors %}
   {% if temp_vendor != vendor | slice: 0 %} <-- How come this statement always be true?
      {% assign temp_vendor = vendor | slice: 0 -%}
      {%- assign add_anchor = true %}

   {% else %}

      {%- assign add_anchor = false %}
   {% endif %}

<div class="brandlist_card"
   {% if add_anchor %} id="{{ vendor | slice:0 }}"{% endif %}
>

   {{ vendor }}

</div>

{% endfor %}

 

Result:
<div class="brandlist_card" id="A">AAA</div>

<div class="brandlist_card" id="A">AAB</div>

<div class="brandlist_card" id="A">ABC</div>

<div class="brandlist_card" id="B">BBB</div>

<div class="brandlist_card" id="C">CCC</div>

Actually, I expect that the result should be as below:

<div class="brandlist_card" id="A">AAA</div>

<div class="brandlist_card">AAB</div>

<div class="brandlist_card">ABC</div>

<div class="brandlist_card" id="B">BBB</div>

<div class="brandlist_card" id="C">CCC</div>

[loop 1]
vendor is "AAA". 

add_anchor = false
temp_vendor = ""
When temp_vendor NOT EQUAL "AAA"
temp_vendor "AAA" | slice: 0
temp_vendor = "A"
assign add_anchor -> true


[loop 2]

vendor is "AAB"

temp_vendor = "A"
vendor | slice: 0  ("A")
{% if temp_vendor != vendor | slice: 0 %}

The above statement should be FALSE since "A" != "A".

How come "A" !="A" is TRUE?

The next step should be:

   {%- assign add_anchor = false %}

Instead of:

      {% assign temp_vendor = vendor | slice: 0 -%}
      {%- assign add_anchor = true %}

 

Replies 0 (0)