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



为了避免复制/粘贴,我可以对不同的操作使用唯一的视图,我可以使用以下方法之一来做到这一点

$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 ;

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

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

最新更新