Ad

Liquid - If Contains Statement Not Working In For Loop

I have a very basic conditional inside a loop that isn't working:

{% for tag in collections.all.tags %} 
     <li class="mb-1 ">
        <a class="text-white" title="{{ tag }}" target="_blank" rel="nofollow noreferrer" href="#">
            {{ tag | handle }}
            {% if canonical_url contains tag | handle %}
                active
            {% endif %}
        </a>
     </li>
{% endfor %}

Right now, my tag | handle is "dodge" and if I print my {{ canonical_url }} I get https://localhost:3000/collections/all/dodge so my conditional should evaluate to truthy and print the word 'active'.

If I modify my statement to {% if canonical_url contains 'dodge' %} it works fine but I need it to be dynamic. Any suggestions?

Ad

Answer

Set the handle into a variable and use the variable for the comparison instead of the handle.

Ad
source: stackoverflow.com
Ad