簡體   English   中英

Shopify /循環內特定於液體的循環索引

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM