Permalink
Browse files

[#2957] Cleanup

- 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...
1 parent 5c83d03 commit 6940e309b54ea3e4d2b957e3e80735d6e97dfdba @weierophinney weierophinney committed Nov 19, 2012
View
52 library/Zend/View/Helper/Partial.php
@@ -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.
@@ -22,11 +23,17 @@
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
*/
@@ -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);
@@ -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;
+ }
}
View
1 tests/ZendTest/View/Helper/PartialLoopTest.php
@@ -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) {
View
38 tests/ZendTest/View/Helper/PartialTest.php
@@ -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;
/**
@@ -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

0 comments on commit 6940e30

Please sign in to comment.