0

I have a doubt about templates using twig, my problem is the following, I have a page called index.html.twig, which extends from base.html.twig. In index.html.twig there are two include blocks, (one is for the upper menu bar; a navbar and the other one corresponds to the footer) My navbar contains the following options for anonymous users,

--Login--Register--Contact--

Now, if I sign in, I wanted to load a different navbar instead of the previous one, for example

--You're signed as Blablabla-- Edit profile -- Exit --

How can I accomplish this? I mean, to include different blocks based on a session. Do I have to check in the view if the user is logged in? Or do I have to check in the controller if the user has session and then load a different index.html.twig(one for logged in users.) In relation with the last option, I can create an index.html.twig, and an indexLoggedIn.html.twig, and in the controller I can check, if the user is logged in I render indexLoggedIn, otherwise I load index.html.twig. I used the FOSUserBundle for user management. Thanks!

1 Answer 1

1

You could do something like this:

{% if app.user and is_granted("IS_AUTHENTICATED_REMEMBERED") %}
  {{ include('FooBarBundle:Navbar:authenticated.html.twig') }}
{% else %}
  {{ include('FooBarBundle:Navbar:anonymous.html.twig') }}
{% endif %}

See the documentation about when to check for IS_AUTHENTICATED_REMEMBERED and when to use IS_AUTHENTICATED_FULLY. And see one of the many other SO posts for more hints.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.