skip to Main Content

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 }}" 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?

2

Answers


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

    Login or Signup to reply.
  2. First You have to assign value of tag handle to shopify liquid variable & then you will be able to use variable in condition And then every thing will be working fine & dynamic. Thanks !!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search