Twig: loop over group members


I was wondering if there is a way to iterate over all members of a specific group within twig? I wasn’t able to find anything on that topic via google or in the grav documentation.

Your question had me puzzled for some time.

Finally I’ve come up with a small plugin called “Snitch” which will expose information about users and groups to Twig so it can be used in templates.

The first version is on GitHub. Please excuse me for the rough ReadMe file.

The solution to your question can then be accomplished in a template like this:

{% set group = 'paid' %}
{% set group_members = [] %}
{% for username, properties in accounts %}
    {% if group in properties.groups|keys %}
        {% set group_members = group_members|merge([
        ]) %}
    {% endif %}
{% endfor %}
{# Simply dump the result here #}
{{ vardump(group_members) }}

I intend to extend the plugin to other YAML files in config and data directories by setting paths in the plugin config.

All feedback is welcome!