Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

Twig: loop over group members

Started by Paul Richter 7 years ago · 1 replies · 1165 views
7 years ago

Hi,

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.

7 years ago

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:

TWIG
{% set group = 'paid' %}
{% set group_members = [] %}
{% for username, properties in accounts %}
    {% if group in properties.groups|keys %}
        {% set group_members = group_members|merge([
            username
        ]) %}
    {% 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!

👍 1

Suggested topics

Topic Participants Replies Views Activity
General · by jeremycherfas, 4 days ago
3 95 3 hours ago
General · by Old Man Umby, 2 days ago
1 60 2 days ago
General · by Sebastian, 4 days ago
1 81 4 days ago
General · by Andy Miller, 2 weeks ago
0 216 2 weeks ago
General · by ready-4-IT, 6 months ago
1 716 3 weeks ago