[英]Shopify / Liquid specific loop index within loop
在Shopify中,我试图循环浏览一些包含功能标题的元字段。 然后,我需要遍历其他一些元字段,并根据当前循环索引获取功能描述。
这段代码对我来说很好用,但是非常优雅,我敢肯定有一种更好的方法可以达到相同的效果!
{% for field in product.metafields.feature_title %}
<h4>{{ field | last }}</h4>
{% assign i = forloop.index %}
{% if forloop.index == 1 %}
<p>{{ product.metafields.feature_description.001 }}</p>
{% endif %}
{% if forloop.index == 2 %}
<p>{{ product.metafields.feature_description.002 }}</p>
{% endif %}
{% if forloop.index == 3 %}
<p>{{ product.metafields.feature_description.003 }}</p>
{% endif %}
{% if forloop.index == 4 %}
<p>{{ product.metafields.feature_description.004 }}</p>
{% endif %}
{% if forloop.index == 5 %}
<p>{{ product.metafields.feature_description.005 }}</p>
{% endif %}
{% endfor %}
另外,存在一个缺陷,该缺陷限制为5,或者如果语句创建了多个对象。
干杯,
D B
未经测试,但类似这样的东西应该起作用:
{% for field in product.metafields.feature_title %}
<h4>{{ field | last }}</h4>
{% capture idx %}00{{forloop.index}}{% endcapture %}
{% assign key = idx | slice: -3, 3 %}
<p>{{ product.metafields.feature_description[key]}}</p>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.