ZF渲染一个动作并在另一个动作中获取html

发布于 2025-01-02 07:32:13 字数 418 浏览 1 评论 0原文

我想要使​​用 Zend Framework 做的是从操作 X 渲染操作 Y 并获取 html:

示例:

public xAction(){
     $html = some_function_that_render_action('y');
}

public yAction(){
     $this->view->somedata = 'sometext';
}

其中 y 视图类似于:

<h1>Y View</h1>
<p>Somedata = <?php echo $this->somedata ?></p>

我找到了操作助手,但无法从控制器使用它。我该如何解决? 有可能吗?

What I want to do with Zend Framework is to render the action Y from the action X and to obtain the html:

Example:

public xAction(){
     $html = some_function_that_render_action('y');
}

public yAction(){
     $this->view->somedata = 'sometext';
}

where the y view is something like:

<h1>Y View</h1>
<p>Somedata = <?php echo $this->somedata ?></p>

I fount the action helper, but I cannot use it from a controller. How can I solve it?
It is possible?

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

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

发布评论

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

评论(3

往事随风而去 2025-01-09 07:32:13

这是做你想做的事情的一种可能的方法。

public function xAction()
{
    $this->_helper
         ->viewRenderer
         ->setRender('y'); // render y.phtml viewscript instead of x.phtml

    $this->yAction();

    // now yAction has been called and zend view will render y.phtml instead of x.phtml
}

public function yAction()
{
    // action code here that assigns to the view.
}

而不是使用 ViewRenderer要设置要使用的视图脚本,您也可以像上面所示那样调用 yAction,但通过调用 $html = 获取 html $this->view->render('controller/y.phtml');

另请参阅 ActionStack 帮助器

Here is one possible way to do what you want.

public function xAction()
{
    $this->_helper
         ->viewRenderer
         ->setRender('y'); // render y.phtml viewscript instead of x.phtml

    $this->yAction();

    // now yAction has been called and zend view will render y.phtml instead of x.phtml
}

public function yAction()
{
    // action code here that assigns to the view.
}

Instead of using the ViewRenderer to set the view script to use, you could also call yAction as I showed above, but get the html by calling $html = $this->view->render('controller/y.phtml');

See also the ActionStack helper.

禾厶谷欠 2025-01-09 07:32:13

您可以从控制器使用 Action View Helper

public function xAction()
{
    $html = $this->view->action(
        'y',
        $this->getRequest()->getControllerName(),
        null,
        $this->getRequest()->getParams()
    );
}

public function yAction()
{
    // action code here that assigns to the view.
}

它不是很漂亮,但效果很好,而且您不必使用 $view->setScriptPath($this->view->getScriptPaths());

这个助手为 yAction() 创建一个新的 Zend_Controller_Request,因此您可以将自己的参数作为第四个参数或使用$this->getRequest()->getParams() 用于传播xAction()的请求参数。

http://framework .zend.com/manual/1.12/en/zend.view.helpers.html#zend.view.helpers.initial.action

You can use the Action View Helper from the controller

public function xAction()
{
    $html = $this->view->action(
        'y',
        $this->getRequest()->getControllerName(),
        null,
        $this->getRequest()->getParams()
    );
}

public function yAction()
{
    // action code here that assigns to the view.
}

It's not very beautiful but it works well and you don't have to use $view->setScriptPath($this->view->getScriptPaths());

This helper creates a new Zend_Controller_Request for yAction(), so you can give your own parameters as 4th argument or use $this->getRequest()->getParams() to spread the request parameters of xAction().

http://framework.zend.com/manual/1.12/en/zend.view.helpers.html#zend.view.helpers.initial.action

无声静候 2025-01-09 07:32:13

最后我找到了这个“解决方案”,这不是我想做的,但它有效,如果有人找到真正的解决方案,请在这里回答。

public function xAction(){
    $data = $this->_prepareData();
    $view = new Zend_View();
    $view->somedata = $data;
    $view->setScriptPath($this->view->getScriptPaths());

    $html = $view->render('controller/y.phtml');
}

Finally I found this "solution", it's not what I want to do, but it works, if someone found the real solution, please answer here.

public function xAction(){
    $data = $this->_prepareData();
    $view = new Zend_View();
    $view->somedata = $data;
    $view->setScriptPath($this->view->getScriptPaths());

    $html = $view->render('controller/y.phtml');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文