2012-02-01 40 views
4

我想与Zend框架做的是从动作X渲染动作Y和获得HTML:ZF渲染的动作,并取得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> 

我发现那位动作助手,但我不能从一个控制器使用。我该如何解决它? 有可能吗?

+0

ZF是否支持'partials'?让xAction和yAction调用其特定模板,并在这些特定模板中调用共享的部分模板。 – 2012-02-01 17:55:12

回答

2

这里有一种可能的方式来做你想做的事。

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 = $this->view->render('controller/y.phtml');

又见ActionStack helper得到的HTML。

+0

您的第一个解决方案不能解决我的问题:它改变了操作的视图,但我只需要html。 – 2012-02-02 09:25:31

+0

您的第二个解决方案返回html,但不要将正确的数据分配给视图(不要调用操作)。 – 2012-02-02 09:26:40

0

最后我发现这个“解决方案”,这不是我想要做的,但它的工作原理,如果有人找到真正的解决方案,请在这里回答。

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

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

调用新的Zend_View()重置视图,并且在其他文件夹和命名空间中注册的帮助器必须重新定义 – 2014-01-20 11:15:02

1

您可以从控制器

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());

此帮助创建一个新的Zend_Controller_Request for yAction(),所以你可以给你自己的参数作为第四个参数或者使用$this->getRequest()->getParams()来传播xAction()的请求参数。

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