BaHar AyØub
BaHar AyØub

Reputation: 337

Go to the next parameter Twig

I'm new on Twig and i would like to know how to go to the next value in a loop

This is a simple example:

{% for user in users %}
        <table>
          <tr>
            <td>
                {{ user.username }}
            </td>

            <td>
                {# here i want to print the next username in the same line of the table #}
            </td>
          </tr>
        </table>
    {% endfor %}

And thanks for your help Sorry for my bad english

Upvotes: 0

Views: 454

Answers (2)

0x1gene
0x1gene

Reputation: 3458

Be sure to check if the user is not the last one before displaying the next one

{% for user in users %}
<table>
     <tr>
          <td>
           {{ user.username }}
          </td>
          <td>
               {% if not loop.last%}
                    {{ users[loop.index0 + 1].username }}
               {%endif%}
          </td>
     </tr>
</table>
{% endfor %}

Upvotes: 0

Adam Hopkinson
Adam Hopkinson

Reputation: 28793

I believe you can do

{{ user[loop.index + 1].username }}

There's some further information about the loop variable at http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable

Upvotes: 1

Related Questions