Shopify / Liquid specific loop index within loop -
in shopify trying cycle through metafields contain feature titles. need cycle through other metafields , feature description based on current loop index.
this code works fine me, inelegant , i'm sure there better way achieve same result!
{% field in product.metafields.feature_title %} <h4>{{ field | last }}</h4> {% assign = 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 flaw limited 5, or whoever many if statements creates.
cheers,
db
not tested should work:
{% 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 %}