Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count relations inside loop in twig

I use Symfony2 and twig templating. Think of the Q&A exact same as stackoverflow. There are list of questions with the count of score, answers, views and so on. How to count the answers of the qeustions inside loop in twig? There are OneToMany relation between Question and Answer tables.

{% for question in questions %}
    <li>{{ question.score }}</li>
    <li>{# there should be the count // count($question->getAnswers()) #}</li>
    <li>{{ question.view }}</li>
{% endfor %}

Or if there is any better way to achieve this, I am open to suggestions.

like image 780
seferov Avatar asked Mar 07 '12 09:03

seferov


1 Answers

This will print the count:

{{ question.answers|length }}
like image 59
Ohas Avatar answered Sep 21 '22 05:09

Ohas