8

I'm trying to print out value of the variable passed to the twig template. I'm using this code:

{{ naziv[0] }} Index is 0 because passed array has only one element. Mentioned code produces following error:

Key "0" for array with keys "title" does not exist in...

but when I use for loop like this:

{% for key,value in naziv %}
{{ value }}
{% endfor %}

I get what I want.

What's wrong with {{naziv[0]}} ?

3
  • what does {{ dump(naziv) }} get you? Commented Jan 7, 2013 at 15:33
  • array(1) { ["title"]=> string(11) "SpaceVision" } Commented Jan 7, 2013 at 15:34
  • Your array is not number indexed, thus naziv[0] is not defined. Commented Jan 7, 2013 at 15:38

2 Answers 2

23

Based on the var_dump of array(1) { ["title"]=> string(11) "SpaceVision" }

You should access your array in this way: {{ naziv['title'] }}.

The key of your array is associative and not a numerically indexed array. That is why you cannot use naziv[0].

You can also use: {{ naziv.title }} aswell.

See the documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you it helped, i had somesort of different problem but your variable['keyname'] method helped me.
5

Your array is not number indexed, thus naziv[0] is not defined. Access it as naziv.title.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.