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

Accessing grav.user.Level?

Solved by Vadym View solution

Started by Tony Shaw 3 years ago · 1 replies · 452 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 Jerry Hunt, 4 days ago
2 76 7 hours ago
General · by pamtbaau, 13 hours ago
1 47 12 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago