shopify - Shopify Liquid - 字符串上的 Forlooping
问题描述
我刚刚开始使用 shopify 并遇到了我的第一个问题,我无法通过 shopify 文档或谷歌搜索来解决。在我看来,我的目标很简单,但给我带来了很多麻烦。所以就在这里。
使用液体模式,我创建了一个input
text(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 允许我循环字符串的长度,但不能访问它的各个部分。
如果我的建议是不可能的,有没有办法将字符串动态拆分为数组。
好久没有发帖了,如果忘记或者发错了,请见谅。感谢您提供的任何帮助,我被困住了,因此任何想法都值得赞赏。
解决方案
您需要在循环之前拆分字符串并将其放入数组中。
因此,您只需要执行以下操作:
{% assign text_arr = section.settings.input | split: '' %}
哪里split: ''
会拆分每个字符。然后你循环text_arr
代替。
推荐阅读
- r - 将 RasterLayer 标准化为矩阵以用作剪辑帧
- java - 如何模拟 KafkaAdminClient
- c++ - 从成员初始化器列表调用基本方法
- sql - Django ORM在注释多个聚合列时删除不需要的Group by
- c++ - 在运行时更改模板参数 c++
- python - 文本块比较python
- java - 登录前检查用户数据
- javascript - Jquery index error Uncaught TypeError: $(...).index(...).remove is not a function
- javascript - 如何根据某些条件映射对象的键值以生成节点映射?
- sql - SQL在表A中查找记录而不在表B中