Zend Framework:在一个布局中渲染多个视图 [英] Zend Framework: Render multiple Views in one Layout

查看:81
本文介绍了Zend Framework:在一个布局中渲染多个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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内呈现news/list ()->内容.

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 is than a kind of a page layout - and auth/login and news/list a kind of widget

该怎么做?

推荐答案

第一个建议是不惜一切代价避免使用Action视图帮助程序,无论如何它可能会在ZF 2.0中被删除. ( ZF-5840 )(

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 Framework:在一个布局中渲染多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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