How to capture if current vendor is selected

一笑奈何 提交于 2019-12-23 04:50:13

问题


In my sidebar on the collection pages and index, I have listed all vendors with the code below. Now I want to add an .active class to the <li> when that particular vendor is selected. I want to do the same thing with a list of product types. How do I check which vendor is selected?

I tried {% if collection.handle == vendor %}. But it returns null as it can only capture collections names.

<ul class="nav">
  {% for vendor in shop.vendors %}
      <li class="collection-container {% if collection.handle == vendor %}active {% endif %}">{{ vendor | link_to_vendor }}</li>
  {% endfor %}
</ul>

Please note that the url is constructed as /collections/types?q=Nike and that after the = comes the vendor. I want to try to somehow capture on which current collection the user is navigating and add an active tag throughout the foreach loop.

I realise it can be done by creating collections for each vendor and product type and using collection.handel, but I am interested in trying to solve it by capturing the last bit of the URL.


回答1:


**show vendor filter use these code **
<ul class="filter">
{% for Vendor in shop.vendors %}
{% if shop.vendors contains Vendor %}
{% if collection.current_vendor == Vendor %} <li class="active"> {{ Vendor | link_to_vendor }}</li>
{% else %}
<li>{{ Vendor | link_to_vendor }}</li>{% endif %}
{% endif %}
{% endfor %}
</ul>

if you want to short vendor try these code
<ul class="filter">
{% assign myvendor = 'samsung,xaomi,Nokia' | split:"," %}
{% for Vendor in myvendor %}
{% if shop.vendors contains Vendor %}
{% if collection.current_vendor == Vendor %} <li class="active"> {{ Vendor | link_to_vendor }}</li>
{% else %}
<li>{{ Vendor | link_to_vendor }}</li>{% endif %}
{% endif %}
{% endfor %}
</ul>



来源:https://stackoverflow.com/questions/54308670/how-to-capture-if-current-vendor-is-selected

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!