FOSUserBundle, global arguments / function before templating

冷暖自知 提交于 2019-12-24 17:53:17

问题


I'm a beginner with Symfony2, and FOSUserBundle, although I'm loving it so far. As of right now, I can override existing templates (such as layout.html.twig), to conform /login, /profile, etc, to the style of the rest of my site. However, prior to use FOSUserBundle, my twig got what links are in the navbar and other widgets from php controllers. So my twig knows nothing about having a home page link or accounts page, e.g. my base.html.twig loops through an array of various objects to render. In the past, I would pass these in as variables when calling twig, but now that FOSUserBundle does all the work, these variables are unpopulated.

One solution is to override all of the FOSUserBundle controllers, find the places where they call the templating engine, and hook in global functions to get the current navbar, get the current status bar, etc. Another solution would be to have twig know about all the potential widgets, and switch based off the app.user information. Really, what I want is to have FOSUserBundle append some information before handing off to twig. I'd like to do this without having to modify or override a bunch of code. Thanks!


回答1:


  • You could embed a controller action with {% render %}.
  • Use globals to inject the menuitems in all templates that twig renders.
  • Use a function to get all the menuitems and render it with a partial with {% include %}
  • Create your own tag {% myAwesomeMenu %}


来源:https://stackoverflow.com/questions/11388694/fosuserbundle-global-arguments-function-before-templating

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!