Php PARTIALOOP中的Zend View数组变量

Php PARTIALOOP中的Zend View数组变量,php,zend-framework,partial,zend-view,Php,Zend Framework,Partial,Zend View,以下是我的控制器中的代码: $this->view->myArray = array(); $this->view->test = ""; $out = $this->view->partialLoop('tab/partial.phtml', $data); echo $this->view->test; // Output: This works echo count($this->view->myArray); // Outp

以下是我的控制器中的代码:

$this->view->myArray = array();
$this->view->test = "";
$out = $this->view->partialLoop('tab/partial.phtml', $data);

echo $this->view->test;  // Output: This works
echo count($this->view->myArray); // Output: 0
以及partial.phtml:


我认为从partialoop访问视图变量不是一个好主意。除此之外,为什么它对我的数组变量不起作用呢?

也许你不能设置$v的值或项目,因为它是私有的、静态的或被丢弃的 另外,从您发布的代码来看,它使用了递归,这可能会使它更容易破坏(即控制器正在引用视图数据,而视图正在设置它,或者没有设置它,或者已经设置了两次)

同意,我不认为从partiallop访问视图var是个好主意

编辑: $this->view->assign('variablename',$myu数组)


我认为变量在重新渲染器上“丢失”,所以在控制器中处理变量,并在完成之前将它们分配给视图。我不会真的对$this->view->myArray执行数组操作,因为您无法访问部分中的视图变量,所以它不起作用。您可以访问传递给分部的数据

$out = $this->view->partialLoop('tab/partial.phtml', $data);
这行代码可以访问
$data
中包含的信息

因此,当前部分中的代码基本上没有意义:

$v = $this->partialLoop()->view; //you choose to assign view data to the partial, and I don't think it's working as expected. 
//By not passing any args to the partial you have at least some access to the view object.

$this->view->test = "This works";//assign data to view locally
echo $v->test;  // you seem to be echoing out locally assigned data

$v->myArray[] = "hello";//you didn't assign this to the view
echo count($v->myArray); // myArray probably doesn't exist in this context or dosen't work as expected. If you make this an associative array it might work.
我想我以前从来没有见过这样使用partials。局部视图的目的是为视图的特定部分建立不同的变量范围

partial和PARTIALLOP是视图帮助程序,因此您需要在控制器中执行的唯一操作(数据可能是或也可能来自模型)是使您想在partial中使用的任何数据以及您想在正常视图范围中使用的任何数据可用

//in a controller
public function userAction() {
    $model = Application_Model_DbTable_User();//Table columns = id, name, role
    $this->view->partailData = $model->fetchAll();//assign data to view that we want to use in partial, should be an array or object.
}

//in a view script
<?php 
//pass the path to the partial as the first arg and the data to be displayed as the second arg
echo $this->partialLoop('/path/to/partial.phtml', $this->partialData);
//we could pass data explicitly as well
echo $this->partial('/path/to/partial.phtml', array('id'=>1,'name'=>'jason','role'=>'user'));
?>

//now for our partial.phtml
//this could be used a simple partial or as a partialLoop
<p>My name is <?php echo $this->name ?>.</p>
<p>My data file id is <?php echo $this->id ?>.</p>
<p>My access control role is <?php echo $this->role ?>. </p>
<!-- name, id and role would be column names that we retrieved from the database and assigned to the view -->
//在控制器中
公共函数userAction(){
$model=Application\u model\u DbTable\u User();//表列=id、name、role
$this->view->partailData=$model->fetchAll();//将数据分配给要在partial中使用的视图,应该是数组或对象。
}
//在视图脚本中
//现在来看partial.phtml
//这可以用作一个简单的partial或partiallop
我的名字是

我的数据文件id为

我的访问控制角色是

要使用partiallop或partiallop,需要传递某种类型的数组或实现toArray()的对象

[编辑]

清理仍在左侧字段中的代码

//controller code
$this->view->myArray = array();

//view code
<?php $v = $this->partial()->view ?> 
<?php $v->myArray[] = 'newName' ?>
<?php Zend_Debug::dump(count($this->partial()->view->myArray)) ?>

//my output =

int(1)
//控制器代码
$this->view->myArray=array();
//视图代码
//我的输出=
int(1)
如果我分配给实际的部分脚本并尝试输出视图对象,则我似乎无法进一步传递该视图。如果抛出以下错误:

//my view again
<?php echo $this->partial('partial.phtml', $this->partial()->view) ?>
//This and attempts similar result in the error
/*Catchable fatal error: Object of class Zend_View could not be converted to string in E:\www\home-local\application\views\scripts\partial.phtml on line 1*/
//when the partial.phtml looks like
<?php echo $this />

//however when I access the data available in the view
<?php echo $this->myArray[0] ?>

//the result works and the output is   
newName 
//再次查看我的视图
//这将导致类似的错误结果
/*可捕获的致命错误:Zend_View类的对象无法转换为第1行E:\www\home local\application\views\scripts\partial.phtml中的字符串*/
//当partial.phtml看起来像
//结果是有效的,输出是有效的
新名称
当您已经有权访问视图对象时,空的partial()(partiallop())调用将授予您访问视图对象的权限。如果离开视图对象的作用域,则只有由_get()和_call()提供的对当前作用域的访问权限


我希望我能够充分解释这一点以提供帮助。

我稍微修改了示例,以说明如果不是数组,则从partial设置变量实际上是可行的。不管怎样,这里没有递归,也没有静态变量。对不起,当我更新这个问题时,我无意中颠倒了一些代码。请再看一看。
//my view again
<?php echo $this->partial('partial.phtml', $this->partial()->view) ?>
//This and attempts similar result in the error
/*Catchable fatal error: Object of class Zend_View could not be converted to string in E:\www\home-local\application\views\scripts\partial.phtml on line 1*/
//when the partial.phtml looks like
<?php echo $this />

//however when I access the data available in the view
<?php echo $this->myArray[0] ?>

//the result works and the output is   
newName