/** * Renders a template fragment within a variable scope distinct from the * calling View object. * * If no arguments are provided, returns object instance. * * @param string $name Name of view script * @param array $values Variables to populate in the view * @throws Exception\InvalidArgumentException * @return string */ public function __invoke($name = null, $values = null) { if (0 == func_num_args()) { return $this; } if (!is_array($values) && (!$values instanceof Traversable) && (is_object($values) && !method_exists($values, 'toArray')) ) { throw new Exception\InvalidArgumentException('PartialLoop helper requires iterable data'); } if (is_object($values) && (!$values instanceof Traversable) && method_exists($values, 'toArray') ) { $values = $values->toArray(); } if ($values instanceof Iterator) { $values = ArrayUtils::iteratorToArray($values); } // reset the counter if it's called again $this->partialCounter = 0; $content = ''; foreach ($values as $item) { $this->partialCounter++; $content .= parent::__invoke($name, $item); } return $content; }
/** * Renders a template fragment within a variable scope distinct from the * calling View object. * * If no arguments are provided, returns object instance. * * @param string $name Name of view script * @param string|array $module If $model is empty, and $module is an array, * these are the variables to populate in the * view. Otherwise, the module in which the * partial resides * @param array $model Variables to populate in the view * @return string * @throws Exception\InvalidArgumentException */ public function __invoke($name = null, $module = null, $model = null) { if (0 == func_num_args()) { return $this; } if (null === $model && null !== $module) { $model = $module; $module = null; } if (!is_array($model) && !$model instanceof Traversable && (is_object($model) && !method_exists($model, 'toArray'))) { throw new Exception\InvalidArgumentException('PartialLoop helper requires iterable data'); } if (is_object($model) && !$model instanceof Traversable && method_exists($model, 'toArray')) { $model = $model->toArray(); } $content = ''; // reset the counter if it's called again $this->partialCounter = 0; foreach ($model as $item) { // increment the counter variable $this->partialCounter++; $content .= parent::__invoke($name, $module, $item); } return $content; }
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); } }
/** * Renders a template fragment within a variable scope distinct from the * calling View object. * * @param string $name Name of view script * @param array $values Variables to populate in the view * @throws Exception\InvalidArgumentException * @return string */ public function loop($name = null, $values = null) { // reset the counter if it's called again $this->partialCounter = 0; $content = ''; foreach ($this->extractViewVariables($values) as $item) { $this->nestObjectKey(); $this->partialCounter++; $content .= parent::__invoke($name, $item); $this->unNestObjectKey(); } return $content; }
public function __invoke($nome = null, $valor = null) { switch ($nome) { case "titulo": if (is_array($valor) == true) { echo "<pre>"; echo "não pode ser um array para partial titulo"; echo "</pre>"; exit; } return p::__invoke("titulo", is_array($valor) ? $valor : array("titulo" => $valor)); break; default: return p::__invoke($nome, $valor); break; } }
public function testPassingNoArgsReturnsHelperInstance() { $test = $this->helper->__invoke(); $this->assertSame($this->helper, $test); }