开发者

What Zend View Partial setObjectKey do?

i am reading zend framework docs on zend view partials

If your model is an object, you may want to have it passed as an object to the partial script, instead of serializing it to an array of variables. You can do t开发者_Python百科his by setting the 'objectKey' property of the appropriate helper:

// Tell partial to pass objects as 'model' variable
$view->partial()->setObjectKey('model');

but what does this do. when do i use it and how.


I'm not 100% positive on this, but from what I can tell by looking at the source and documentation is that standard behavior for rendering a partial is that values are passed into it in the form of an associative array. This allows the values to be bound to variables using array keys.

echo $this->partial('partial.phtml', array ('person' => 'joe');

// in my partial..
<h1><?php echo $this->person; ?></h1>  //<h1>Joe</h1>

If you pass an object as the third parameter, (ie, partial('partial.phtml', $myobject);), Zend_View_Partial will automatically serialize that object in an associative array, either by a custom implementation of toArray() or it will just grab the public properties via get_object_vars().

However, if you want to pass the whole object, as an object, you need to set the array key that gets transformed into a variable for the partial to reference.

$this->partial()->setObjectKey('myobject');
echo $this->partial('partial.phtml', $myobject);

What benefits this approach has over partial('partial.phtml', array( 'myobject' => $myobject), I'm not sure. Or I could be interpreting the documentation wrong.


Key as in array(key => value)?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜