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

clean way to show some content only if someone is logged in?

twig

Started by Anna 3 days ago · 5 replies · 98 views
3 days ago

I understand that twig in content is a lot more restrictive now, for security reasons. So I’m currently doing this in a custom twig template with {% if not grav.user.authenticated %}, but I feel content really should go in the pages and not in a twig template. So what is the clean way to do this now?

Edit: I wrapped the content bits in section tags (named “authenticated” and “unauthenticated” respectively) and plugged those into my custom template. That is so far an acceptable solution, though I would still love to hear if there’s a better way.

Adding to that, how do I show the login form for unauthenticated users? If I put {% include 'partials/login-form.html.twig' } %} in my template, nothing shows up. If I do {% include 'partials/login-form.html.twig' with { show_login_form: true } %}, the remember me checkbox and the buttons turn up, but the username and password fields are still missing. Not sure how I should be doing this…

last edited 07/20/26 by Anna
2 days ago

I am making a tutorial site that is only available to logged in users. So for most of the pages I did it as you suggested, but for the home page I would like to have a little bit of text for people who are not yet logged in and a login form. And once they’re logged in, there should be a different text.

Like I said, I solved the problem with the different bits of text quite neatly, but I can’t add the login form directly, only a link to it. And that’s an extra click that should be avoided.

…Hmm, but that’s an idea: I could add my „show if home page“ logic to the login form template… that might be a neat way to do it! Will report back later 😃

2 days ago

Sadly, this does not work easily, because page.home is always false (since it’s the login page). How could I check whether the login form came from/redirects to the home page? I can’t find that information in the page object… out of ideas for now.

5 hours ago

Oh, I managed to miss those! Very nice, thank you.

I’m still left with the problem of showing the login form directly though… apparently there is no simple solution for that?

Hm… I will probably put some information on the login page regardless where it turns up then. Will give it a try and report back.

Suggested topics

Topic Participants Replies Views Activity
Support · by TomW, 2 hours ago
1 11 2 hours ago
Support · by Anna, 3 days ago
7 89 5 hours ago
Support · by Anna, 5 hours ago
0 8 5 hours ago
Support · by stuart young, 2 days ago
2 65 2 days ago
Support · by BenLaKnet, 2 weeks ago
3 215 3 days ago