Zend 重用视图

发布于 2024-11-15 11:33:35 字数 586 浏览 3 评论 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”。我该如何正确地做到这一点?

For every new controller and action I create, Zend expects a template file in /views/scripts/controllername. However, I would like to have multiple actions share a single template into which text from a database can be injected.

I currently use a layout and delegate page specific views with echo $this->layout()->content. I tried the following:

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');
    }
}

However, I get an error that the script 'some/some.phtml' not found in path. How do I do this correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

树深时见影 2024-11-22 11:33:35

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

$this->render('default');

请参见 Zend_Controller_Action::render

render( string|null $action = null, string|null $name = null, bool $noController = false ) : void

渲染视图

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

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


如果您想提供特定的脚本,请使用

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

参见 Zend_Controller_Action::renderScript

renderScript( string $script, string $name = null ) : void

渲染给定的视图脚本

与{@link render()}类似,此方法渲染视图脚本。然而,与 render() 不同的是,它不会通过 {@link getViewScript()} 自动确定视图脚本,而是渲染传递给它的脚本。如果您知道要使用的确切视图脚本名称和路径,或者使用不符合 getViewScript() 定义的规范的路径,请使用此选项。

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

You have to use 'default' (without the extension) and call the method directly (not on the view), e.g.

$this->render('default');

See Zend_Controller_Action::render

render( string|null $action = null, string|null $name = null, bool $noController = false ) : void

Render a view

Renders a view. By default, views are found in the view script path as /.phtml. You may change the script suffix by resetting {@link $viewSuffix}. You may omit the controller directory prefix by specifying boolean true for $noController.

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


If you want to provide a specific script, use

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

See Zend_Controller_Action::renderScript

renderScript( string $script, string $name = null ) : void

Render a given view script

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.

几味少女 2024-11-22 11:33:35

如果您选择使用 viewRenderer 并且需要从另一个控制器中选择视图,则必须将 $noController 参数设置为 TRUE:

BookController:

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

这将查找视图 < em>/views/scripts/approval/index.phtml 而不是默认的 /views/scripts/book/save-book.phtml

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文