Search code examples
htmlshopifyliquidstore

How to add an active class with if statement in Liquid (Shopify)


I have 2 links inside the header (one link takes you to one stores the other link takes you to another store). I need to add an active class to the store the user is on (bmx-store & skate-store are the 2 classes that need active class adding to). So if the user is on the bmx store, the link for that store becomes bold and underlined and vice versa.

{% if request.url contains 'source-bmx-testing.myshopify.com' %}

{% elsif request.url contains 'sourceskate.myshopify.com' %}

{% endif %}

I know the code needs to be similar to the snippet above but not sure how to add it into the code i have to add the active class to the store depending on which one you are on. Below is my code:

            {%- for block in section.blocks -%}
              {%- case block.type -%}
              {%- when 'bmx-link' -%}
                <a class="link-container" href="{{ block.settings.store_link }}">
                  <div class="bmx-store">
                    <img 
                      width="15"
                      height="15"
                      src="{{ block.settings.icon_image | img_url: '15x15' }}"
                      srcset="{{ block.settings.icon_image | img_url: '15x15' }}"
                      alt="{{ block.settings.icon_image.alt }}">
                    <span>{{ block.settings.store_link_title }}</span>
                  </div>
                </a>
                {%- when 'skate-link' -%}
                  <a class="link-container" href="{{ block.settings.store_link }}">
                    <div class="skate-store">
                      <img 
                        width="15"
                        height="15"
                        src="{{ block.settings.icon_image | img_url: '15x15' }}"
                        srcset="{{ block.settings.icon_image | img_url: '15x15' }}"
                        alt="{{ block.settings.icon_image.alt }}">
                      <span>{{ block.settings.store_link_title }}</span>
                    </div>
                  </a>
              {%- endcase -%}
              {%- endfor -%}


Solution

  •           {% assign bmx_class = 'bmx-store' %}
              {% assign skate_class = 'skate-store' %}
              {% if shop.name == 'bmx' %} <!-- use youre store name here -->
                   {% assign bmx_class = 'bmx-store current-store' %}
              {% elsif shop.name == 'skate' %}
                   {% assign skate_class = 'skate-store current-store %}
              {% endif %}
              {%- for block in section.blocks -%}
              {%- case block.type -%}
              {%- when 'bmx-link' -%}
                 {% assign class_block = bmx_class %}
              {%- when 'skate-link' -%}
                 {% assign class_block = skate_class %}
              {% endcase %}
             
                <a class="link-container" href="{{ block.settings.store_link }}">
                  <div class="{{ class_block }}">
                    <img 
                      width="15"
                      height="15"
                      src="{{ block.settings.icon_image | img_url: '15x15' }}"
                      srcset="{{ block.settings.icon_image | img_url: '15x15' }}"
                      alt="{{ block.settings.icon_image.alt }}">
                    <span>{{ block.settings.store_link_title }}</span>
                  </div>
                </a>
              
              {%- endfor -%}
    

    If stores are more than 2 other cleaner solutions can be found.