Count of Party Members

It generally isn’t very interesting to simply show a list of Parties in a page, with no other information.

Showing how many MPs I know about for each party proved to be slightly tricky, but eventually doable:
{% for party in %}
  {{ }}
  {% assign mps = 0 %}
  {% for person in %}
    {% for membership in person.memberships %}
      {% if membership.organization_id == %}
        {% assign mps = mps | plus:1 %}
      {% endif %}
    {% endfor %}
  {% endfor %}
  ({{ mps }})
{% endfor %}

For each party we know about, we start by knowing of zero MPs for them. Then we loop through every MP in people.json, and check if any of their memberships are for that party. And if so we use jekyll’s plus:1 filter to increment that count, which we then display at the end.

However, that means if the same person has been an MP at two separate times they get counted twice, which probably isn’t what we want. So we also need to add an

 {% assign counted = 0 %}

just before the

  {% for membership in person.memberships %} loop

and then change the counting logic to make sure we haven’t already counted them:

  {% if counted == 0 and membership.organization_id == %}

[see final code]

What I really want is to then sort the list by the number of members, but I couldn’t find a way to do that…

Leave a Reply

Your email address will not be published. Required fields are marked *