简体   繁体   English

Shopify /循环内特定于液体的循环索引

[英]Shopify / Liquid specific loop index within loop

In Shopify I am trying to cycle through some metafields that contain feature titles. 在Shopify中,我试图循环浏览一些包含功能标题的元字段。 Then I need to cycle through some other metafields and get the feature description based on the current loop index. 然后,我需要遍历其他一些元字段,并根据当前循环索引获取功能描述。

This code works just fine for me, but it is very inelegant and I'm sure there is a much better way to achieve the same result! 这段代码对我来说很好用,但是非常优雅,我敢肯定有一种更好的方法可以达到相同的效果!

{% 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 %}

Additionally there is a flaw that this is limited to 5, or whoever many if statements creates. 另外,存在一个缺陷,该缺陷限制为5,或者如果语句创建了多个对象。

Cheers, 干杯,

DB D B

not tested but something like this should work: 未经测试,但类似这样的东西应该起作用:

{% 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM