【发布时间】: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 语句创建的。
干杯,
数据库
【问题讨论】: