unfoudev
unfoudev

Reputation: 87

Twig: Access to a specific array field

I have a database table that contain 5 fields.

Table(id,A,B,C,D,E); // the Id is Auto_increment.

In it I have 2 lines , then I execute

$recup=$app['db']->executeQuery("SELECT * FROM postit");
$result = $recup->fetchAll();

to get all data.

In return I send ( I'm using Silex framework )

return $app['twig']->render('accueil.twig',array('postits'=>$result));

Now in Twig I would like get ,for example, C field of the second line. I tried

{% for  user  in postits%}

    {%for key,  useruser in user %}
        {{useruser}}
    {%endfor%}
{%endfor%}

It prints

1 title hugo 602 186 texttext 2 title2 hugo2 188 132 texttxet2

I would for exemple juste print 1 or hugo etc..

Upvotes: 0

Views: 1669

Answers (1)

DarkBee
DarkBee

Reputation: 15633

You can access arrays fields in twig just like you access them in php:

{% for user in postits %}
   Using square brackets: {{ user['A'] }}<br />
   Using dot: {{ user.id }}<br />
{%endfor%}

Upvotes: 2

Related Questions