/** * {@inheritdoc} */ public function getRender($template = null, array $params = []) { $this->isInitialized || $this->initialize(); if (empty($template)) { $className = (new \ReflectionObject($this))->getShortName(); $template = mb_strtolower($className); } $view = new View(); $view->setDI($this->getDI()); $view->setViewsDir($this->getViewPath()); return $view->getPartial($template, array_merge($this->getViewParams(), $params)); }
public function testPartials() { $view = new View(); $view->setBasePath(__DIR__ . '/../'); $view->setViewsDir('unit-tests/views/'); $view->setParamToView('cool_var', 'le-this'); // Single partial $view->start(); $view->render('test5', 'index'); $view->finish(); $this->assertEquals($view->getContent(), '<html>Hey, this is a partial, also le-this</html>' . PHP_EOL); // Multiple partials $view->start(); $view->render('test9', 'index'); $view->finish(); $this->assertEquals($view->getContent(), '<html>Hey, this is a partial, also le-this<br />Hey, this is a second partial, also le-this</html>' . PHP_EOL); // A partial within other partial $view->start(); $view->render('test5', 'subpartial'); $view->finish(); $this->assertEquals('<html>Including Hey, this is a partial, also le-this</html>' . PHP_EOL, $view->getContent()); // Single partial in overridden main view $view->setMainView('html5'); $view->start(); $view->render('test5', 'index'); $view->finish(); $this->assertEquals('<!DOCTYPE html><html>Hey, this is a partial, also le-this</html>' . PHP_EOL, $view->getContent()); // Retrieve a partial as a string $this->assertEquals('Hey, this is a partial, also le-this', $view->getPartial('partials/_partial1', array('cool_var' => 'le-this'))); }
/** * Tests rendering with partials * * @author Andres Gutierrez <*****@*****.**> * @since 2012-05-28 */ public function testRenderingWithPartials() { $this->specify('The rendering with partials does not work as expected', function () { $view = new View(); $view->setViewsDir(PATH_DATA . 'views' . DIRECTORY_SEPARATOR); $view->setParamToView('cool_var', 'le-this'); $view->start(); $view->render('test5', 'index'); $view->finish(); expect($view->getContent())->equals("<html>Hey, this is a partial, also le-this</html>\n"); $view->start(); $view->render('test9', 'index'); $view->finish(); expect($view->getContent())->equals("<html>Hey, this is a partial, also le-this<br />Hey, this is a second partial, also le-this</html>\n"); $view->start(); $view->render('test5', 'subpartial'); $view->finish(); expect($view->getContent())->equals("<html>Including Hey, this is a partial, also le-this</html>\n"); $view->setMainView('html5'); $view->start(); $view->render('test5', 'index'); $view->finish(); expect($view->getContent())->equals("<!DOCTYPE html><html>Hey, this is a partial, also le-this</html>\n"); expect($view->getPartial('partials/_partial1', ['cool_var' => 'le-this']))->equals('Hey, this is a partial, also le-this'); }); }