FOSUserBundle,模板之前的全局参数/函数 [英] FOSUserBundle, global arguments / function before templating

查看:67
本文介绍了FOSUserBundle,模板之前的全局参数/函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Symfony2和FOSUserBundle的初学者,尽管到目前为止我还是很喜欢.截至目前,我可以覆盖现有模板(例如layout.html.twig),以使/login,/profile等与网站其余部分的样式保持一致.但是,在使用FOSUserBundle之前,我的树枝从php控制器获取了navbar和其他小部件中的链接.因此,我的树枝对拥有首页链接或帐户页面一无所知,例如我的base.html.twig遍历了一系列要渲染的对象.过去,我在调用twig时会将它们作为变量传递给我,但是现在FOSUserBundle完成了所有工作,因此这些变量不再存在.

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.

一种解决方案是覆盖所有FOSUserBundle控制器,找到它们调用模板引擎的位置,并挂接全局函数以获取当前导航栏,获取当前状态栏等.另一种解决方案是使用树枝了解所有潜在的小部件,并根据app.user信息关闭.确实,我想要的是让FOSUserBundle在移交给细枝之前附加一些信息.我想这样做而不必修改或覆盖一堆代码.谢谢!

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!

推荐答案

  • 您可以在{% render %}中嵌入控制器动作.
  • 使用全局变量在细枝渲染的所有模板中注入菜单项.
  • 使用函数获取所有菜单项,并使用{% include %}
  • 进行部分渲染
  • 创建您自己的标签{% myAwesomeMenu %}
    • 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 %}
    • 这篇关于FOSUserBundle,模板之前的全局参数/函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆