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

Accessing grav.user.Level?

Solved by Vadym View solution

Started by Tony Shaw 4 years ago · 1 replies · 720 views
4 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?

4 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 jeremycherfas, 1 week ago
4 181 2 days ago
General · by lynbor, 6 days ago
2 122 3 days ago
General · by Old Man Umby, 1 week ago
1 100 1 week ago
General · by Sebastian, 1 week ago
1 124 1 week ago
General · by Andy Miller, 3 weeks ago
0 248 3 weeks ago