Steven Moffat
Steven Moffat

Reputation: 461

TWIG - loop through a string

I have a variable page.stock which is set on the load as the string "3,4,5,6".

I want to loop through this variable. I tried:

{% for mysize in app.request.get(page.stock) %}

    <input type="radio" id="{{mysize}}" name="size" value="{{mysize}}" >
    <label for="{{mysize}}">{{mysize}}</label> 

{% endfor %}

and I also tried:

{% for mysize in page.stock %}

    <input type="radio" id="{{mysize}}" name="size" value="{{mysize}}" >
    <label for="{{mysize}}">{{mysize}}</label>   

{% endfor %}

Both with no luck. How do I iterate through the , delimited string?

Upvotes: 5

Views: 4270

Answers (1)

Paul
Paul

Reputation: 141839

You'll need to split the string into a list:

{% for mysize in page.stock|split(',') %}
   <input type="radio" id="{{mysize}}" name="size" value="{{mysize}}" >
   <label for="{{mysize}}">{{mysize}}</label> 
{% endfor %}

Upvotes: 6

Related Questions