Zend 框架:在一个布局中渲染多个视图 [英] Zend Framework: Render multiple Views in one Layout
问题描述
我想使用 Zend_Layout 生成一个动态站点.
I want to generate a dynamic site using Zend_Layout.
我的布局 (/application/layouts/scripts/layout.phtml) 包含以下几行:
My layout (/application/layouts/scripts/layout.phtml) contains the following lines:
...
<body>
<?php echo $this->render('header.phtml') ?>
<div id="content"><?php echo $this->layout()->content ?></div>
<?php echo $this->render('footer.phtml') ?>
</body>
...
如果我浏览到我的索引控制器索引操作 - Zend 会自动呈现 $this->layout()->content 内的索引视图 (application/views/scripts/index/index.phtml).
If i browse to my index controller index action - Zend renders the index view (application/views/scripts/index/index.phtml) inside of $this->layout()->content automatically.
现在我想在布局中呈现不同控制器动作的视图.因此,我生成了一个新的控制器身份验证,其中包含一个显示登录表单的操作登录.
Now i want to render the views of to different controller actions in the layout. So i generate a new controller auth with an action login that shows a login form.
我将布局更改为:
...
<body>
<?php echo $this->render('header.phtml') ?>
<div id="content"><?php echo $this->layout()->content ?></div>
<div id="login"><?php echo $this->layout()->login ?></div>
<?php echo $this->render('footer.phtml') ?>
</body>
...
当我浏览到索引/索引时,我想在这个动作中定义zend应该在 $this->layout()->login 内呈现 auth/login 视图,例如 $this->layout 内的新闻/列表()-> 内容.
When i browse to index/index, i want to define in this action that zend should render auth/login view inside $this->layout()->login and for example news/list inside of $this->layout()->content.
index/index 不仅仅是一种页面布局 -和 auth/login 和 news/list 一种小部件
index/index is than a kind of a page layout - and auth/login and news/list a kind of widget
如何做到这一点?
推荐答案
第一个建议是不惜一切代价避免使用 Action 视图助手,无论如何它可能会在 ZF 2.0 中被删除.(ZF-5840) (为什么 actionstack 是邪恶的)
First advice is to avoid the Action view helper at all costs, it will probably be removed in ZF 2.0 anyway. (ZF-5840) (Why the actionstack is evil)
这与我问的问题有关 - 和 bittarman 的回答很有用.实现类似功能的最佳方法是拥有一个可以生成登录"区域的视图助手.My_View_Helper_Login
例如.然后您的布局可以调用 $this->login()
,user/login
的视图脚本也可以.至于让 index/index
呈现来自 news/list
的内容,只需将请求从控制器转发到另一个控制器/动作.$this->_forward('list', 'news');
This is related to a question I asked - and bittarman's answer is pretty useful. The best way to implement something like that is to have a view helper that can generate your "login" area. My_View_Helper_Login
for instance. Then your layout can call $this->login()
, and so can the view script for user/login
. As far as having index/index
render the content from news/list
just forward the request to the other controller/action from the controller. $this->_forward('list', 'news');
这篇关于Zend 框架:在一个布局中渲染多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!