【发布时间】:2018-04-18 00:13:09
【问题描述】:
是否可以在for循环语句中分配一个变量并使用该变量?
我正在尝试将产品集合放在博客中,而不必为需要集合的每篇文章创建新的 for 循环。
我所做的是使用文章的标签并在集合句柄之前进行拆分,以便我可以将其注入 for 循环中,但它还不能完全动态地工作......
这就是我所做的......我在文章标签中添加了
collection_some-page-handle然后在文章.liquid
{% for tag in article.tags %}
{% assign tagMap = tag | split: '_' %}
{% if tagMap.first == 'collection' %}
{% assign collectionName = tagMap.last %}
{% endif %}
{% endfor %}
{{collectionName}} <!-- this shows the handle of the collection -->
{% for product in collections.collectionName.products limit: 8%}
<div class="productWrap">
<a href="{{ product.url}}"><img src="{{ product.featured_image | product_img_url: 'medium' }}" alt=""></a>
<a href="{{product.url}}"><p class="product_title" style="border-top:1px solid #efefef;">{{ product.title | split: ' - ' | first }}</p></a>
</div>
{%endfor%}
现在,如果我尝试将变量放在 for 循环中它不起作用,但当然如果我放入实际句柄它会起作用。有没有办法动态地做到这一点?
【问题讨论】: