Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TWIG - loop through a string

Tags:

twig

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?

like image 697
Steven Moffat Avatar asked May 22 '13 16:05

Steven Moffat


1 Answers

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 %}
like image 100
Paul Avatar answered Oct 01 '22 20:10

Paul