title: Count the number of items in an array using Twig while in an IF statement
body: <p>I'm building an help centre for an application and I want to be able to display the number of topics within a specific category. At the moment, this is what I have:</p>
  
  <pre><code>{% for cat in cats %}
      &lt;div class="panel panel-default"&gt;
          &lt;div class="panel-heading"&gt;
              &lt;h4 class="panel-title"&gt;
                  &lt;a class="accordion" data-toggle="collapse" data-parent="#helpcategories" href="#category{{cat.id}}"&gt;
                      {{cat.category}}
                      {% for top in tops %}
                          {% if top.category == cat.id %}
                              &lt;span class="badge pull-right"&gt;
                                  {{ tops|length }}
                              &lt;/span&gt;
                          {% endif %}
                      {% endfor %}
                  &lt;/a&gt;
              &lt;/h4&gt;
          &lt;/div&gt;
          &lt;div id="category{{cat.id}}" class="panel-collapse collapse"&gt;
              &lt;div class="panel-body"&gt;
                  &lt;ul class="nav nav-pills nav-stacked"&gt;
                      {% for top in tops %}
                          {% if top.category == cat.id %}
                              &lt;li&gt;&lt;a href="#" class="list-group-item"&gt;{{top.title}}&lt;/a&gt;&lt;/li&gt;
                          {% endif %}
                      {% endfor %}
                  &lt;/ul&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  {% endfor %}
  </code></pre>
  
  <p>As you can see, I use Twig to sort out the topics in to their respective categories. As you can also see, in the area that I want to display the number of topics within a category I am using <code>{{tops|length}}</code>. However, this returns the number of topics in total, not per category.</p>
  
  <p>How can I get Twig to count the number of times a topic appears in a category?</p>