问题
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