Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
[#2957] Cleanup
Browse files Browse the repository at this point in the history
- To keep backwards compatibility:
  - re-instated "object key" functionality
- For forwards compatibility:
  - allow specifying a view model as the sole argument
  - pull variables from the view model when passed as the second argument
  • Loading branch information
weierophinney committed Nov 19, 2012
1 parent 5c83d03 commit 6940e30
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
52 changes: 49 additions & 3 deletions library/Zend/View/Helper/Partial.php
Expand Up @@ -12,6 +12,7 @@

use Traversable;
use Zend\View\Exception;
use Zend\View\Model\ModelInterface;

/**
* Helper for rendering a template fragment in its own variable scope.
Expand All @@ -21,12 +22,18 @@
*/
class Partial extends AbstractHelper
{
/**
* Variable to which object will be assigned
* @var string
*/
protected $objectKey;

/**
* Renders a template fragment within a variable scope distinct from the
* calling View object. It proxies to view's render function
*
* @param string $name Name of view script
* @param array $values Variables to populate in the view
* @param string|ModelInterface $name Name of view script, or a view model
* @param array|object $values Variables to populate in the view
* @return string|Partial
* @throws Exception\RuntimeException
*/
Expand All @@ -36,10 +43,19 @@ public function __invoke($name = null, $values = null)
return $this;
}

// If we were passed only a view model, just render it.
if ($name instanceof ModelInterface) {
return $this->getView()->render($name);
}

if (is_scalar($values)) {
$values = array();
} elseif ($values instanceof ModelInterface) {
$values = $values->getVariables();
} elseif (is_object($values)) {
if (method_exists($values, 'toArray')) {
if (null !== ($objectKey = $this->getObjectKey())) {
$values = array($objectKey => $values);
} elseif (method_exists($values, 'toArray')) {
$values = $values->toArray();
} else {
$values = get_object_vars($values);
Expand All @@ -48,4 +64,34 @@ public function __invoke($name = null, $values = null)

return $this->getView()->render($name, $values);
}

/**
* Set object key
*
* @param string $key
* @return Partial
*/
public function setObjectKey($key)
{
if (null === $key) {
$this->objectKey = null;
return $this;
}

$this->objectKey = (string) $key;
return $this;
}

/**
* Retrieve object key
*
* The objectKey is the variable to which an object in the iterator will be
* assigned.
*
* @return null|string
*/
public function getObjectKey()
{
return $this->objectKey;
}
}
1 change: 1 addition & 0 deletions tests/ZendTest/View/Helper/PartialLoopTest.php
Expand Up @@ -221,6 +221,7 @@ public function testShouldNotCastToArrayIfObjectIsTraversable()
$view = new View();
$view->resolver()->addPath($this->basePath . '/application/views/scripts');
$this->helper->setView($view);
$this->helper->setObjectKey('obj');

$result = $this->helper->__invoke('partialLoopObject.phtml', $o);
foreach ($data as $item) {
Expand Down
38 changes: 38 additions & 0 deletions tests/ZendTest/View/Helper/PartialTest.php
Expand Up @@ -12,6 +12,7 @@

use PHPUnit_Framework_TestCase as TestCase;
use Zend\View\Helper\Partial;
use Zend\View\Model\ViewModel;
use Zend\View\Renderer\PhpRenderer as View;

/**
Expand Down Expand Up @@ -131,6 +132,43 @@ public function testPassingNoArgsReturnsHelperInstance()
$test = $this->helper->__invoke();
$this->assertSame($this->helper, $test);
}

public function testCanPassViewModelAsSecondArgument()
{
$model = new ViewModel(array(
'foo' => 'bar',
'bar' => 'baz',
));

$view = new View();
$view->resolver()->addPath($this->basePath . '/application/views/scripts');
$this->helper->setView($view);
$return = $this->helper->__invoke('partialVars.phtml', $model);

foreach ($model->getVariables() as $key => $value) {
$string = sprintf('%s: %s', $key, $value);
$this->assertContains($string, $return);
}
}

public function testCanPassViewModelAsSoleArgument()
{
$model = new ViewModel(array(
'foo' => 'bar',
'bar' => 'baz',
));
$model->setTemplate('partialVars.phtml');

$view = new View();
$view->resolver()->addPath($this->basePath . '/application/views/scripts');
$this->helper->setView($view);
$return = $this->helper->__invoke($model);

foreach ($model->getVariables() as $key => $value) {
$string = sprintf('%s: %s', $key, $value);
$this->assertContains($string, $return);
}
}
}

class Aggregate
Expand Down

0 comments on commit 6940e30

Please sign in to comment.