部分循环中的Zend视图数组变量


Zend View array variable inside a partialLoop

这是我的控制器中的代码:

$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

和partialpartial.phtml:

$v->test = $this->partialLoop()->view;
$v = "This works";
echo $v->test;  // Output: This works
$v->myArray[] = "hello";
echo count($v->myArray); // Output: 0

我不认为从partialLoop访问视图变量是个好主意。除此之外,为什么它不适用于我的数组变量?

它不起作用,因为您无法访问分部中的视图变量。您可以访问传递给分部的数据。

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

我想我以前从未见过以这种方式使用偏音符。分部的目的是为视图的特定部分建立一个不同的变量范围。

partial和partialLoop是视图助手,因此您需要在控制器中执行的唯一操作(数据可能是模型,也可能来自模型)是使您希望在局部中使用的任何数据以及您希望在正常视图范围中使用的所有数据可用。

//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 -->

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

[EDIT]

清理你的代码你仍然在左栏。

//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)

如果我分配给一个实际的部分脚本并试图输出视图对象,那么我似乎无法再传递视图了。抛出错误:

//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 

当您已经可以访问视图对象时,一个空的partial()(partialLoop())调用将允许您访问视图对象。如果离开视图对象的作用域,则只能访问__get()和__call()提供的当前作用域。

我希望我能够对此做出足够的解释。

您可能无法设置$v或项的值,因为它是私有的、静态的或已丢弃的从你发布的代码来看,它使用递归,这可能会使它更容易破坏(即控制器正在引用视图数据,视图正在设置或尚未设置或已设置两次)

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

编辑:$this->view->assign('variablename',$my_array);

我认为变量在Rerender上"丢失"了,所以在控制器中处理变量,然后在完成之前将它们分配给视图。我不会真正在$this->view->myArray 上执行数组操作