Gc\View\Helper\Partial::__invoke PHP Méthode

__invoke() public méthode

Returns script from identifier.
public __invoke ( string $name = null, array $values = [] ) : mixed
$name string Name of view script
$values array Variables to populate in the view
Résultat mixed
    public function __invoke($name = null, $values = array())
    {
        if (empty($name)) {
            return $this;
        }
        $view = $this->cloneView();
        $this->assignVars($view, $values);
        try {
            $viewModel = ViewModel::fromIdentifier($name);
        } catch (\Exception $e) {
            //don't care
        }
        if (empty($viewModel)) {
            return $view->render($name);
        }
        $name = 'view/' . $name;
        return $view->render($name);
    }

Usage Example

Exemple #1
0
 /**
  * Test
  *
  * @return void
  */
 public function testInvoke()
 {
     $model = $this->getMockForAbstractClass('Gc\\Core\\Object');
     $model->setData(array('foo' => 'bar', 'bar' => 'baz'));
     $templatePathStack = $this->object->getView()->resolver();
     $templatePathStack->addPath(__DIR__ . '/_files/views');
     //With object
     $this->object->partialCounter = true;
     $return = $this->object->__invoke('partial-vars.phtml', $model);
     $this->object->partialCounter = false;
     foreach ($model->toArray() as $key => $value) {
         $string = sprintf('%s: %s', $key, $value);
         $this->assertContains($string, $return);
     }
     //With array
     $return = $this->object->__invoke('partial-vars.phtml', array('foo' => 'bar', 'bar' => 'baz'));
     foreach ($model->toArray() as $key => $value) {
         $string = sprintf('%s: %s', $key, $value);
         $this->assertContains($string, $return);
     }
     //With object
     $model = new stdClass();
     $model->foo = 'bar';
     $model->bar = 'baz';
     $return = $this->object->__invoke('partial-vars.phtml', $model);
     foreach (get_object_vars($model) as $key => $value) {
         $string = sprintf('%s: %s', $key, $value);
         $this->assertContains($string, $return);
     }
     //With object
     $this->object->setObjectKey('foo');
     $model = new stdClass();
     $model->foo = 'bar';
     $model->bar = 'baz';
     $return = $this->object->__invoke('partial-obj.phtml', $model);
     $this->assertNotContains('No object model passed', $return);
     $this->assertInstanceOf('Gc\\View\\Helper\\Partial', $this->object->__invoke(''));
     $this->assertEquals('View Content', $this->object->__invoke('view-identifier'));
     $this->setExpectedException('Zend\\View\\Exception\\RuntimeException');
     $this->assertFalse($this->object->__invoke('fake-view-identifier'));
 }