2010年2月21日星期日

Re: [fw-mvc] Layout will not recognize view data passed to it from controller

Zend_Layout uses a different view instance, so you can't use $this->view->varname to assign to the layout's view. Your method works, but there is a shortcut you can use from within controllers:

// in controller
$this->_helper->layout()->varname = 'var';

In your layout script, you can access the variable like this:

// in layout
echo $this->layout()->varname;

--
Hector


On Sun, Feb 21, 2010 at 12:18 PM, Andy Daykin <daykinandy@gmail.com> wrote:
Hello,
 
I used the batch file to setup my directory structure for the MVC application with version 1.10.1. So I have the same file structure as in the quick start demo on the framework site. In my controller I am trying to send variables to my layout, like so:
 
$this->view->varname = "Var"
 
and then echo them in my layout like:
 
echo $this->varname
 
I am able to get this working in my files in the application/views/scripts/*/*.phtml files, but in my layout which is in application/layouts/layout.phtml, this does not work. In order to get variables to my layout I have to use:
 

Zend_Layout::getMvcInstance()->assign(

'varname', 'Var');

 

How can I access the view data in my layout the same way I am in my view files?

 
In my bootstrap file I have the following:
 

<?php

class

Bootstrap extends Zend_Application_Bootstrap_Bootstrap

{

protected function _initAutoload()

{

$moduleLoader = new Zend_Application_Module_Autoloader(array(

'namespace' => '',

'basePath' => APPLICATION_PATH));

return $moduleLoader;

}

protected function _initViewHelpers()

{

$this->bootstrap('layout');

$layout = $this->getResource('layout');

$view = $layout->getView();

$view->doctype('XHTML1_STRICT');

$view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');

$view->headTitle()->setSeparator(' - ');

$view->headTitle('Test');

}

protected function _initDb()

{

$dbResource = $this->getPluginResource("db");

$db = $dbResource->getDbAdapter();

Zend_Registry::set(

"db", $db);

}

}

 

Thanks,

 

-Andy Daykin


没有评论: