Razvan.432
Razvan.432

Reputation: 663

Symfony 2 twig split function is not working as expected

I am bored searching for an answer so this is my firs question here.

In Symfony 2 , in my twig template i iterate over an array of objects:

 {% for client in clients %}

i have the variable client.curs = to the string "Cursul 1 = 4.1234" i want to split this string so i use

  {% set cursarr = client.curs|split(' = ') %}

now, if i dump my array i get array (size=2) 0 => string 'Cursul 1' (length=8) 1 => string '4.1234' (length=6) wow! cool! just what i wanted. i continue my work, i just need the second part of the array (4.1234) so i do this:

  {{ cursarr[1] }}

ooops! Key "1" for array with keys "0" does not exist.

Ok! i am an idiot so i try:

{{ cursarr.1 }} same error here. Hmmmm! WTF?!

i try {{ cursarr[0] }} pops out 'Cursul 1' WTF?!

{{ cursarr.0 }} also working

i don't get it, what am i doing wrong? Why is life so complicated? is it because it is late and i am tired? Need help!

 {% endfor %}

Upvotes: 0

Views: 690

Answers (1)

Razvan.432
Razvan.432

Reputation: 663

I manage to make it work this morning with a clear mind :)

{% set cursarr = client.cursuri|split(' = ') %}
                 {% set cSpecial = '' %}
                 {% for curs in cursarr %}
                     {% set cSpecial = curs %}   
                 {% endfor %}

I have to iterate the array in order to get the second value; I didn't realized it is a multidimensional mdfkr. Hope this will help someone in need.

Upvotes: 0

Related Questions