Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Twig: loop over group members

Started by Paul Richter 7 years ago · 1 replies · 1002 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 Jerry Hunt, 4 days ago
2 95 15 hours ago
General · by pamtbaau, 20 hours ago
1 61 19 hours ago
General · by Andy Miller, 1 day ago
0 47 1 day ago
General · by Marcel, 12 months ago
6 356 5 days ago
General · by Duc , 6 days ago
3 44 6 days ago