首页 > 解决方案 > Shopify Liquid - 字符串上的 Forlooping

问题描述

我刚刚开始使用 shopify 并遇到了我的第一个问题,我无法通过 shopify 文档或谷歌搜索来解决。在我看来,我的目标很简单,但给我带来了很多麻烦。所以就在这里。

使用液体模式,我创建了一个inputtext(string) 类型的变量,其值为 no importants。重要的是能够循环遍历input变量,以便我可以以动态方式查看每个字符。

尝试的变体:

{% for char in section.settings.input %} // Loop 1
    {{ char }} <-- char is never displayed -->
{% endfor %}

{% for input_idx in (0..section.settings.input.size) %} // Loop 2
    <div class="example 1">{{section.settings.input[input_idx]}}</div>
    <div class="example 2">{{section.settings.input[forloop.index]}}</div>
    <div class="example 3">{{section.settings.input | split: input_idx}}</div>
{% endfor %}

结论

到目前为止,在尝试的每个变体中,我都无法隔离字符串的字符。Loop 2 允许我循环字符串的长度,但不能访问它的各个部分。

如果我的建议是不可能的,有没有办法将字符串动态拆分为数组。

好久没有发帖了,如果忘记或者发错了,请见谅。感谢您提供的任何帮助,我被困住了,因此任何想法都值得赞赏。

标签: shopifyliquidshopify-template

解决方案


您需要在循环之前拆分字符串并将其放入数组中。

因此,您只需要执行以下操作:

{% assign text_arr = section.settings.input | split: '' %}

哪里split: ''会拆分每个字符。然后你循环text_arr代替。


推荐阅读