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

Accessing grav.user.Level?

Solved by Vadym View solution

Started by Tony Shaw 3 years ago · 1 replies · 649 views
3 years ago

In a user's *.yaml file (in user/accounts) I notice the "Level:" property defaults to "Visitor" when someone registers on the frontend using root/user_register/ (the login plugin route). Of course, this can be changed to something else like "Member" in Admin.

What I am trying to do is write a little twig logic into the body of a default page based on this "Level" property attached to the current (logged in) user.

Twig has no problem accessing {{grav.user.username}}, and {{grav.user.email}}, but not {{grav.user.Level}}. I'd really like to make this work but I've run out of ideas. What's the solution?

3 years ago Solution

Hi, @boatful
You can access user objects only if the user is logged in. If you assigned new values or parameters to the user you must logged out/in to see the difference, and maybe clear the cache. But probably you just missed the case sensitivity. After all, the example below works as it should.

TWIG
{% if grav.user.level is same as ('Visitor') %}
    {# do what you need #}
    <h1>You are a Visitor</h1>
{% endif %}

but I'd rather use user groups instead since they're easier to manage via admin. Let's say we create a group named "member".

TWIG
{% for group in grav.user.groups %}
    {% if group is same as ('member') %}
        {# do what you need #}
        <h1>You are a Member</h1>
    {% endif %}
{% endfor %}

Suggested topics

Topic Participants Replies Views Activity
General · by Andy Miller, 2 days ago
0 77 2 days ago
General · by Veehem, 3 days ago
0 113 3 days ago
General · by milkboy, 4 days ago
0 114 4 days ago
General · by ian russell, 2 weeks ago
2 205 1 week ago
General · by pamtbaau, 1 month ago
2 462 2 weeks ago