将变量发送到 Zend Framework 中的布局
在我的项目中,我有许多动态元素,它们在每个页面上都是一致的。我已将这些放入我的layout.phtml
我的问题是:如何从我的控制器将变量发送到我的布局中?
如果我想从我的控制器发送东西,我可以使用:
$this->view->whatever = "foo";
并在视图中接收它,
echo $this->whatever;
我不知道如何对我的布局执行相同的操作。也许有更好的方法来解决这个问题?
In my project I have a number of dynamic elements that are consistently on every page. I have put these in my layout.phtml
My question is: How can I send variables into my layout from my controllers?
If I want to send things from my controller I can use:
$this->view->whatever = "foo";
And receive it in the view with
echo $this->whatever;
I cannot figure out how to do the same with my layout. Perhaps there is a better way around the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
布局是一个视图,因此分配变量的方法是相同的。在您的示例中,如果您要在布局中 echo $this->whatever ,您应该看到相同的输出。
一个常见的问题是如何将每个页面上使用的变量分配给布局,因为您不希望在每个控制器操作中重复代码。解决此问题的一种方法是创建一个插件,在渲染布局之前分配此数据。例如:
然后向前端控制器注册这个插件,例如
Zend_Controller_Front::getInstance()->registerPlugin(new My_Layout_Plugin());
The layout is a view, so the method for assigning variables is the same. In your example, if you were to echo $this->whatever in your layout, you should see the same output.
One common problem is how to assign variables that you use on every page to your layout, as you wouldn't want to have to duplicate the code in every controller action. One solution to this is to create a plugin that assigns this data before the layout is rendered. E.g.:
then register this plugin with the front controller, e.g.
Zend_Controller_Front::getInstance()->registerPlugin(new My_Layout_Plugin());
不使用助手或插件:
此后您可以在布局中使用以下内容:
这将打印“foo”。
Without using helpers or plugins do :
After this you can use the following in your layout:
This will print "foo".
我已经实现了一个所有其他控制器都扩展的基本控制器。
所以我有一个控制器......
并且在布局和/或视图中
I have a implemented a Base Controller which all other controllers extend.
So I have a controller...
and in the layout and/or view
如果您使用 MVC 中的布局,则标准视图变量可用。在引导文件中,包括以下内容:
然后,您必须告诉每个控制器(甚至每个操作,如果您想对几个不同的布局进行精细控制)要使用哪个布局。我把我的放在每个控制器的 init() 中。下面是一个示例,如果您的布局文件名为layout.phtml:
The standard view variables are available if you use the layout within the MVC. In bootstrap file, include this:
You must then tell each controller (or even each action, if you wanted granular control over several different layouts) which layout to use. I put mine in the init() of each controller. Here's an example, if your layout file is named layout.phtml:
好吧,我想您可以通过创建视图助手来获得另一种解决方案。在 application/views/helper 中创建一个文件并将其命名为您想要的
abc.php
然后将以下代码放在那里。所以你可以在布局中使用这个助手,例如..
Well i guess you can have another solution by creating view helper.. create a file in application/views/helper and name it what ever you want
abc.php
then put the following code over there.So you can use this helper in layout like..
可以在某些操作中调用If :
在布局文件中,如果您不需要该部分,则
In the layout file you can call
If in some actions if you don't want that section then:
附带说明一下,如果您在应用程序中的某个时刻发送 json,请注意全局视图变量不会随响应一起发送。
As a side note, if you send json at some point in your app be careful that global view variables are not sent with the response.
查看助手也是一个好主意。我有一个电子商务网站,其中有一个layout.phtml,其中包含我需要从数据库中获取的类别和子类别的菜单。
为此,我执行了以下操作:
Bootstrap.php:
application.ini:
在视图/帮助程序中,我有一个名为菜单的文件:
在layout.phtml中,我只需调用特定的帮助程序,并从中调用方法:
希望它可以帮助需要从数据库获取数据来呈现布局的人。
View Helpers are also a good idea. I had a ecommerce website, which I had a layout.phtml with menus with categories and subcategories that I needed to bring from the database.
For this, I did the following:
Bootstrap.php:
application.ini:
In views/helpers, I had a file called Menus:
In layout.phtml, I just had to call the specific helper, and call the methods from it:
Hope it helps someone that needs to bring data from database to render the layout.