是否可以分配一个变量并在for循环语句中使用该变量?
我试图在博客中放置一个产品集合,而不必为需要集合的每一篇文章创建一个新的for循环。
我所做的是在集合句柄之前使用文章的标记并进行拆分,这样我就可以将其注入for循环,但它还不能动态工作。。。
以下是我所做的。。我在文章中添加了标签
collection_some-page-handle
然后在文章中。液体
{% 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循环,它将不起作用,但如果我放入实际句柄,它当然会起作用。有没有一种方法可以动态执行?