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

Twig: loop over group members

Started by Paul Richter 7 years ago · 1 replies · 1049 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 Hanns Mattes, 3 weeks ago
1 271 3 weeks ago
General · by Andy Miller, 3 weeks ago
0 196 3 weeks ago
General · by Jerry Hunt, 3 weeks ago
2 334 3 weeks ago
General · by pamtbaau, 3 weeks ago
1 255 3 weeks ago
General · by Andy Miller, 3 weeks ago
0 229 3 weeks ago