【问题标题】:Shopify / Liquid specific loop index within loop循环内的 Shopify / Liquid 特定循环索引
【发布时间】:2016-06-01 21:21:54
【问题描述】:

在 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 个,或者任何多个 if 语句创建的。

干杯,

数据库

【问题讨论】:

    标签: shopify liquid


    【解决方案1】:

    未经测试,但类似这样的东西应该可以工作:

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

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 2017-05-28
      • 2020-01-28
      • 2015-11-25
      • 2011-02-19
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多