簡體   English   中英

Shopify Liquid-在字符串上循環

[英]Shopify Liquid - Forlooping on Strings

我剛剛開始使用shopify,並且遇到了我的第一個問題,盡管shopify文檔或Google搜索無法解決。 我的目標很簡單,但給我帶來了很多麻煩。 就是這樣

使用液體模式,我創建了一個類型為text(string)的變量input ,其值不重要。 重要的是能夠遍歷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 %}

結論

到目前為止,在所有嘗試的變體中,我都無法隔離字符串的字符。 循環2允許我循環字符串的長度,但不能訪問字符串的各個部分。

如果我的建議是不可能的,那么有一種方法可以將字符串動態拆分為and。

我已經很久沒有發布信息了,如果我忘記或出錯了,請對不起。 感謝您提供的任何幫助,敬請見諒。

您需要先拆分字符串並將其放入數組,然后再循環。

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

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

split: ''將拆分每個字符。 然后,您改為循環text_arr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM