【发布时间】:2019-04-02 13:12:38
【问题描述】:
在创建 Shopify 主题时,我在创建循环时遇到了问题。
我允许主题管理员从 1 到 4 个产品中选择在商店中展示。他可以从他的主题的自定义 UI 中选择它们。
架构是:
{
"type": "product",
"id": "popular_product_1",
"label": "Product N°1"
},
{
"type": "product",
"id": "popular_product_2",
"label": "Product N°2"
},
{
"type": "product",
"id": "popular_product_3",
"label": "Product N°3"
},
{
"type": "product",
"id": "popular_product_4",
"label": "Product N°4"
}
回到我的液体文件,作为测试,如果我想要我可以做的产品的 URL:
{{ all_products[section.settings.popular_product_1].url }}
它会起作用的。但当然,我必须重复相同的代码 4 次。所以我想创建一个循环遍历每个循环。
但是如何获取上面要插入的增量数呢? 当然
{{ all_products[section.settings.popular_product_i].url }}
{{ all_products[section.settings.popular_product_{{i}}].url }}
不工作。
我也试过
{% assign i = 1 %}
{% capture popular_product %}section.settings.popular_product_{{i}}{% endcapture %}
{{ all_products[popular_product].url }}
但它也不起作用......因为变量 Popular_product 似乎是一个字符串,但它不是应该的。
【问题讨论】: