0

对于我创建的每个新控制器和动作,Zend 都需要一个/views/scripts/controllername. 但是,我希望多个操作共享一个模板,可以将数据库中的文本注入到该模板中。

我目前使用布局并使用echo $this->layout()->content. 我尝试了以下方法:

class SomeController extends Zend_Controller_Action{

    public function someAction() {
        $path = $this->view->getScriptPath();
        $this->view = new Zend_View();
        $this->view->setScriptPath($path);
        $this->view->render('default.phtml');
    }
}

但是,我收到一个错误,即在路径中找不到脚本“some/some.phtml”。我该如何正确地做到这一点?

4

2 回答 2

2

您必须使用“默认”(不带扩展名)并直接调用该方法(不在视图上),例如

$this->render('default');

Zend_Controller_Action::render

渲染(字符串|null $action = null,字符串|null $name = null,bool $noController = false):无效

渲染视图

渲染视图。默认情况下,视图在视图脚本路径中作为 /.phtml 找到。您可以通过重置 {@link $viewSuffix} 来更改脚本后缀。您可以通过为 $noController 指定 boolean true 来省略控制器目录前缀。

默认情况下,呈现的内容会附加到响应中。您可以通过指定 $name 来指定要设置的命名正文内容段。


如果要提供特定脚本,请使用

$this->renderScript('controller/action.phtml');

Zend_Controller_Action::renderScript

渲染脚本(字符串 $script,字符串 $name = null):无效

渲染给定的视图脚本

Similar to {@link render()}, this method renders a view script. Unlike render(), however, it does not autodetermine the view script via {@link getViewScript()}, but instead renders the script passed to it. Use this if you know the exact view script name and path you wish to use, or if using paths that do not conform to the spec defined with getViewScript().

By default, the rendered contents are appended to the response. You may specify the named body content segment to set by specifying a $name.

于 2011-06-14T11:05:09.193 回答
0

If you would choose to use viewRenderer and you would need to pick a view from another controller you must set $noController parameter to TRUE:

BookController:

public function saveBookAction()
{ 
    ...
    $this->_helper->viewRenderer('/approval/index', null, $noController = true);
}

This would look for the view /views/scripts/approval/index.phtml instead of the default /views/scripts/book/save-book.phtml.

于 2012-04-29T10:12:35.213 回答