dongxin8392 2015-01-21 17:00
浏览 28
已采纳

Symfony2项目,使用树枝模板设计的问题

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条回答 默认 最新

  • douhao2011 2015-01-21 17:40
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳