Matt Rogers Matt Rogers - 27 days ago 9
Twig Question

Nested Arrays Issue using Twig

This has really stumped me, not sure why I cant get it working.

I have an a variable called 'posts'. Which contains the following.

array(2) { ["data"]=> array(5) {
[0]=> array(3) {
["full_picture"]=> string(98) "https://www.website.com/picture.jpg"
["message"]=> string(613) "long message here"
["id"]=> string(32) "000000000" }
[1]=> array(3) {
["full_picture"]=> string(98) "https://www.website.com/picture.jpg"
["message"]=> string(613) "long message here"
["id"]=> string(32) "000000000" }
[2]=> array(3) {
["full_picture"]=> string(98) "https://www.website.com/picture.jpg"
["message"]=> string(613) "long message here"
["id"]=> string(32) "000000000" }
[3]=> array(3) {
["full_picture"]=> string(98) "https://www.website.com/picture.jpg"
["message"]=> string(613) "long message here"
["id"]=> string(32) "000000000" }
[4]=> array(3) {
["full_picture"]=> string(98) "https://www.website.com/picture.jpg"
["message"]=> string(613) "long message here"
["id"]=> string(32) "000000000" }
}

["paging"]=> array(2) {
["previous"]=> string(324) "website.com/link"
["next"]=> string(306) "website.com/link" }
}


I want to use twig to display the values in the nested arrays within the data array using twig markup.

At the moment I have the follow

{% for post in posts %}
{{ post.data.message }}
{{ endfor }}


I have also tried the following with no success.

{% for key, post in posts %}
{{ post.data.message }}
{{ endfor }}


any guidance on where i am going wrong would be great. Thanks.

Answer

Thanks for all the help everyone. Once again it was a basic error by myself. I forgot to pass the variable through when rendering the page using slim.

The correct markup in the end was very basic

{% for post in posts %}
{{ post.full_picture }}
{{ post.message }}
{% endfor %} 

thanks again!

Comments