【发布时间】:2019-03-05 13:05:57
【问题描述】:
我正在尝试遍历一组产品所属的所有集合。这是我的代码:
<div class="container model-collection">
<h1>{{ collection.title }}</h1>
{% paginate collection.products by 12 %}
<div class="grid collection-products-container">
<ul>
{% for product in collection.products %}
{% for collection in product.collections %}
<li><a href="{{ collection.url }}">{{ collection.title }}</a></li>
{% endfor %}
{% endfor %}
</ul>
</div>
{% if paginate.pages > 1 %}
{% include 'pagination' %}
{% endif %}
{% endpaginate %}
</div>
这很好用,但是如果两个产品属于同一个集合,它会列出该集合两次。所以我需要限制它只显示每个集合一次的循环。 我试过这样做:
<div class="container model-collection">
<h1>{{ collection.title }}</h1>
{% assign model = collection.title %}
<div class="grid collection-products-container">
<ul>
{% for product in collection.products %}
{% assign seen_collections = "" %}
{% for collection in product.collections %}
{% unless seen_collections contains collection %}
{% assign seen_collections = seen_collections | append: "," | append: collection %}
<li><a href="{{ collection.url }}/{{ model }}">{{ collection.title }}</a></li>
{% endunless %}
{% endfor %}
{% endfor %}
</ul>
</div>
</div>
但这只会返回其中一个集合两次,而不会返回其他集合。任何想法如何做到这一点?
【问题讨论】: