Search code examples
shopifyliquidshopify-template

Shopify Liquid - Forlooping on Strings


I have just started using shopify and have run into my first issue I have not been able to solve though the shopify docs or google searches. My goal to my mind a simple one but has given me much trouble. So here it is.

Using liquids schema I have created a variable input of type text(string) with a value of no importants. What is important is the ability to loop through the input variable so that i can look at each character in a dynamic manner.

Variations Tried:

{% 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 %}

Conclusion

So far in every variation tried I can not isolate the characters of the string. Loop 2 allows me to loop the length of the string but not access individual parts of it.

If what I'm suggesting is not possible is there a way to split the string into and array dynamically.

I haven't posted in a long time so sorry if I've forgotten or made a mistake. Thanks for any help I'm stuck so any ideas are appreciated.


Solution

  • You need to split the string and make it in array before looping it.

    So you just need to do the following:

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

    Where split: '' will split every character. Then you loop the text_arr instead.