使用$ this-> renderScript时为视图指定值

时间:2012-06-18 08:35:24

标签: zend-framework

为了避免复制/粘贴,我可以使用独特的视图进行不同的操作,我可以使用其中一种方式来做到这一点

$this->renderScript('index/index.phtml'); 
$this->view->var = "hello world";   

$this->_helper->viewRenderer('index');
$this->view->var = "hello world";

如果我想使用不同的控制器,我必须使用第一个,viewRenderer是好的,但是当我使用renderScript时,它显示没有像var那样定义。如何为视图脚本分配值?

index.phtml会是这样的事情

echo $this->var ;

1 个答案:

答案 0 :(得分:2)

它应该按照你描述的方式工作

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        $this->view->var = 'echo me in any action viewscript and i will show you text';
    }

    public function indexAction()
    {
        // action body
        $this->view->test = 'Don\'t put me here becuase this is not the action that is run';

    }

    public function testAction()
    {
        // action body

        $this->view->test = 'Hello world';
        $this->renderScript('index/index.phtml');
        // or $this->_helper->viewRenderer('index');
    }

}

在我看来(index.phtml)

我有

<?php echo $this->test;?> 

当我去/ index / test /它会显示“hello world”......

当我在另一个控制器中执行此操作时,它会给我结果

class MyController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
        $this->view->test = 'Hello world';
        $this->renderScript('index/index.phtml'); 
    }

}