以下是我的控制器中的代码:
$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 :
$v->test = $this->partialLoop()->view;
$v = "This works";
echo $v->test; // Output: This works
$v->myArray[] = "hello";
echo count($v->myArray); // Output: 0
我不认为从partialLoop访问视图变量是个好主意。除此之外,为什么它不适用于我的数组变量?
答案 0 :(得分:1)
它不起作用,因为您无权访问partial中的视图变量。您可以访问传递给部分数据的数据。
$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()的对象。
<强> [编辑] 强>
将您的代码清理在左侧字段中。
//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()提供的当前范围可用的访问权限。
我希望我能够解释这一点足以帮助。
答案 1 :(得分:-1)
也许您无法设置$ v或项目的值,因为它的私有或静态或被丢弃 也来自您使用递归发布的代码,这可能使它更容易破解(即控制器正在引用视图数据,视图正在设置它或者没有设置它或已经设置了两次)
同意我不认为从partialLoop访问视图var是一个好主意。
编辑: $ this-&gt; view-&gt; assign('variablename',$ my_array);
我认为该变量在Rerender上“丢失”,因此在控制器中处理变量,并在完成之前将它们分配给视图。我不会真的对$ this-&gt; view-&gt; myArray
进行数组操作