简体   繁体   English

Symfony在每次刷新时将变量传递给模板

[英]Symfony Pass variable to template on every refresh

I know there's a few threads on here about passing variables to the template using partials or slots. 我知道这里有一些关于使用局部变量或插槽将变量传递到模板的线程。 My question is, how do you define that variable so that no matter what page you are on, the variable is still accessible. 我的问题是,如何定义该变量,以便无论您在哪个页面上,该变量仍可访问。 I know each one of the actions "extend" sfActions but is there a way to access that and set an action for the site as a whole? 我知道每个动作都会“扩展” sfActions但是有没有办法访问它并为整个站点设置一个动作?

Edit: 编辑:

Every user has a field in the sfUser table that I would like displayed at the top of every page. 每个用户在sfUser表中都有一个字段,我希望在每个页面的顶部显示该字段。 Here is the code I would like to use but I'm not sure where to put or or how to pass it to the partial on the template page. 这是我想使用的代码,但是我不确定将其放置在何处或如何将其传递到模板页面的局部。

$this->user = $this->getUser()->getGuardUser();
$this->activeitems = $this->user->getActiveItems();

In every template, you can access your user information using $sf_user . 在每个模板中,您都可以使用$sf_user访问您的用户信息。 It returns a myUser object. 它返回一个myUser对象。

If you've installed sfGuard, this class should extends sfGuardSecurityUser . 如果已安装sfGuard,则此类应扩展sfGuardSecurityUser And inside this class, you can retrieve the sfGuardUser object using ->getGuardUser() method. 在此类内,您可以使用->getGuardUser()方法检索sfGuardUser对象。

So to retrieve this information inside any templates, you can do: 因此,要在任何模板中检索此信息,可以执行以下操作:

$activeitems = $sf_user->getGuardUser()->getActiveItems();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM