Salman
Salman

Reputation: 49

Print values of array in Twig with nested loop

I am working with symfony 2. I am fetching records from table and sending it to the view: Below is the php code that normally prints each value of the array:

<tbody>
      <?php foreach($categories as $row){?>
         <tr>
           <?php foreach($row as $name => $value){ ?>
               <td><?php echo $value;?></td>
           <?php }?>                                      
         </tr>
      <?php }?>
</tbody>

I am trying to do the same with twig:

<tbody>
    {% for row in categories %}
      <tr>
        {% for cell in row %}
           <td> </td>
        {% endfor %}
      </tr>
    {% endfor %}
</tbody>

Can anyone please tell me what should i put between to get the result?

Upvotes: 0

Views: 3898

Answers (1)

Markus Kottl&#228;nder
Markus Kottl&#228;nder

Reputation: 8268

It's just as easy as use a variabe:

<tbody>
    {% for row in categories %}
      <tr>
        {% for cell in row %}
           <td>{{ cell }}</td>
        {% endfor %}
      </tr>
    {% endfor %}
</tbody>

Or if you want to use the key too:

<tbody>
    {% for row in categories %}
      <tr>
        {% for key, cell in row %}
           <td> </td>
        {% endfor %}
      </tr>
    {% endfor %}
</tbody>

And if cell is an array you can just use cell.yourkey cell.anotherkey.

Upvotes: 1

Related Questions