/** * Render the provided model. * * @param ModelInterface $model * @return string */ public function render(ModelInterface $model) { if ($this->layout && !$model->terminate()) { $this->layout->addChild($model); $model = $this->layout; } // hack, force ZendView to return its output instead of triggering an event // see: http://mateusztymek.pl/blog/using-standalone-zend-view $model->setOption('has_parent', true); return $this->zendView->render($model); }
/** * Processes a view script and returns the output. * * @param string|ModelInterface $nameOrModel The script/resource process, or a view model * @param null|array|\ArrayAccess $values Values to use during rendering * @return string|null The script output. * @throws \Zend\View\Exception\DomainException */ public function render($nameOrModel, $values = array()) { $model = null; if ($nameOrModel instanceof ModelInterface) { $model = $nameOrModel; $nameOrModel = $model->getTemplate(); if (empty($nameOrModel)) { throw new Exception\DomainException(sprintf('%s: received View Model argument, but template is empty', __METHOD__)); } $values = (array) $model->getVariables(); } if (!$this->canRender($nameOrModel)) { return null; } if ($model && $this->canRenderTrees() && $model->hasChildren()) { if (!isset($values['content'])) { $values['content'] = ''; } foreach ($model as $child) { /** @var \Zend\View\Model\ViewModel $child */ if ($this->canRender($child->getTemplate())) { $template = $this->resolver->resolve($child->getTemplate(), $this); return $template->render((array) $child->getVariables()); } $child->setOption('has_parent', true); $values['content'] .= $this->view->render($child); } } /** @var $template \Twig_Template */ $template = $this->resolver->resolve($nameOrModel, $this); return $template->render((array) $values); }
/** * Processes a view script and returns the output. * * @param string|ModelInterface $nameOrModel The script/resource process, or a view model * @param array|\ArrayAccess $values Values to use during rendering * @throws \Zend\View\Exception\DomainException * @return string The script output. */ public function render($nameOrModel, $values = array()) { $model = null; if ($nameOrModel instanceof ModelInterface) { $model = $nameOrModel; $nameOrModel = $model->getTemplate(); if (empty($nameOrModel)) { throw new Exception\DomainException(sprintf('%s: received View Model, but template is empty', __METHOD__)); } $options = $model->getOptions(); foreach ($options as $setting => $value) { $method = 'set' . $setting; if (method_exists($this, $method)) { $this->{$method}($value); } unset($method, $setting, $value); } unset($options); $values = (array) $model->getVariables(); } // check if we can render the template if (!$this->canRender($nameOrModel)) { return null; } // handle tree rendering if ($model && $this->canRenderTrees() && $model->hasChildren()) { if (!isset($values['content'])) { $values['content'] = ''; } foreach ($model as $child) { /** @var \Zend\View\Model\ViewModel $child */ if ($this->canRender($child->getTemplate())) { $file = $this->resolver->resolve($child->getTemplate(), $this); $this->smarty->addTemplateDir(dirname($file)); $childVariables = (array) $child->getVariables(); $childVariables['this'] = $this; $this->smarty->assign($childVariables); return $this->smarty->fetch($file); } $child->setOption('has_parent', true); $values['content'] .= $this->view->render($child); } } // give the template awareness of the Renderer $values['this'] = $this; // assign the variables $this->smarty->assign($values); // resolve the template $file = $this->resolver->resolve($nameOrModel); $this->smarty->addTemplateDir(dirname($file)); // render return $this->smarty->fetch($file); }
/** * @group ZF-5748 */ public function testDisablingLfiProtectionAllowsParentDirectoryTraversal() { $view = new View(array('lfiProtectionOn' => false, 'scriptPath' => __DIR__ . '/_templates/')); try { $test = $view->render('../_stubs/scripts/LfiProtectionCheck.phtml'); $this->assertContains('LFI', $test); } catch (Zend_View_Exception $e) { $this->fail('LFI attack failed: ' . $e->getMessage()); } }
public function test() { /* $viewConfigExample = [ 'some-view' => [ // view name 'template' => 'some-template', // html template //--- not required options 'viewModel' => 'some-view-model|\Some\ViewModel::class', // Instance of ViewModel 'extend' => 'parent-view', // extended view 'capture' => 'some-capture', // for grouping views 'children' => [ 'child-view'// array of views ], 'childrenDynamicLists' => [ // will be generated by list from one of child 'child-view' => 'listVar' // every entry in listVar will be setted to genereted child ], 'data' => [ // required data 'fromGlobal' => 'varName', // // will be set as variables from global data 'fromParent' => 'varName', // will be set as variables by calling getVariable('varName') from parent 'static' => [ // will be set as variables 'key' => 'value' ], ], ], ]; */ $renderer = new PhpRenderer(); $resolver = new Resolver\AggregateResolver(); $map = new Resolver\TemplateMapResolver(array('page' => __DIR__ . '/view/page.phtml', 'comments-list' => __DIR__ . '/view/comments-list.phtml', 'comment' => __DIR__ . '/view/comment.phtml', 'user' => __DIR__ . '/view/user.phtml')); $stack = new Resolver\TemplatePathStack(array('script_paths' => array(__DIR__ . '/view'))); $resolver->attach($map)->attach($stack)->attach(new Resolver\RelativeFallbackResolver($map))->attach(new Resolver\RelativeFallbackResolver($stack)); $renderer->setResolver($resolver); $view = new View(); $response = new Response(); $view->setResponse($response); $strategy = new PhpRendererStrategy($renderer); $strategy->attach($view->getEventManager()); $viewConfig = ['layouts' => ['layout' => ['template' => 'layout']], 'contents' => ['page' => ['layout' => 'layout', 'template' => 'page', 'children' => ['comments-list' => ['extend' => 'comments-list', 'template' => 'comments-list'], 'comment-create' => ['template' => 'comment-create', 'children' => ['myself-info' => ['viewModel' => \Sebaks\ViewTest\MyselfViewModel::class, 'template' => 'user'], 'comment-create-form' => ['template' => 'form', 'children' => ['form-element-textarea' => ['capture' => 'form-element', 'template' => 'form-element-textarea'], 'form-element-button' => ['capture' => 'form-element', 'template' => 'form-element-button']]]]], 'users-table' => ['template' => 'table', 'children' => ['table-head-rows' => ['template' => 'table-tr', 'data' => ['fromParent' => 'rows'], 'children' => ['table-th' => ['template' => 'table-th', 'capture' => 'table-td', 'data' => ['fromParent' => 'value']]], 'childrenDynamicLists' => ['table-th' => 'rows']], 'table-body-rows' => ['template' => 'table-tr', 'data' => ['fromParent' => 'rows'], 'children' => ['table-td' => ['template' => 'table-td', 'data' => ['fromParent' => 'value']]], 'childrenDynamicLists' => ['table-td' => 'rows']]], 'childrenDynamicLists' => ['table-body-rows' => 'bodyRows', 'table-head-rows' => 'headRows'], 'data' => ['static' => ['headRows' => [['Id', 'Name']], 'bodyRows' => [['1', 'John'], ['2', 'Helen']]]]]]]], 'blocks' => ['comments-list' => ['children' => ['comment' => ['viewModel' => \Sebaks\ViewTest\CommentViewModel::class, 'template' => 'comment', 'children' => ['user' => ['viewModel' => \Sebaks\ViewTest\UserViewModel::class, 'template' => 'user', 'data' => ['fromParent' => 'userId', 'static' => ['class' => 'user']], 'children' => ['location' => ['viewModel' => \Sebaks\ViewTest\LocationViewModel::class, 'template' => 'location', 'data' => ['fromParent' => 'countryId']]]]], 'data' => ['fromParent' => ['foo' => 'bar', 'comment' => 'comment']]]], 'childrenDynamicLists' => ['comment' => 'comments'], 'data' => ['fromGlobal' => ['foo' => 'bar', 'result' => 'comments']]]]]; $data = ['result' => [['id' => 'c1', 'userId' => 'u1', 'text' => 'text of c1'], ['id' => 'c2', 'userId' => 'u2', 'text' => 'text of c2']]]; $serviceLocator = new \Zend\ServiceManager\ServiceManager(); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\CommentViewModel::class, \Sebaks\ViewTest\CommentViewModel::class, false); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\UserViewModel::class, \Sebaks\ViewTest\UserViewModel::class, false); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\LocationViewModel::class, \Sebaks\ViewTest\LocationViewModel::class, false); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\MyselfViewModel::class, \Sebaks\ViewTest\MyselfViewModel::class, false); ///////////////////// $config = ['sebaks-view' => $viewConfig]; $serviceLocator->setService('config', $config); $serviceLocator->setService('EventManager', new EventManager()); $request = new Request(); $serviceLocator->setService('Request', $request); //$response = new Response(); $serviceLocator->setService('Response', $response); $e = new MvcEvent(); $e->setRequest($request); $e->setResponse($response); $dispatchResult = new ViewModel(); $dispatchResult->setVariables($data); $e->setResult($dispatchResult); $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName('page'); $e->setRouteMatch($routeMatch); $application = new Application([], $serviceLocator); $e->setApplication($application); ///////////////////// $viewBuilder = new BuildViewListener(); $viewBuilder->injectLayout($e); $pageViewModel = $e->getViewModel(); $view->render($pageViewModel); $result = $response->getBody(); $expected = '<body><ul><li>text of c1 <div class="user">John<span class="location">Ukraine</span></div></li><li>text of c2 <div class="user">Helen<span class="location">United States</span></div></li></ul> <div class="">Me</div><form><textarea></textarea><button type="submit">Submit</button></form><table> <thead> <tr><th>Id</th><th>Name</th></tr> </thead> <tbody> <tr><td>1</td><td>John</td></tr><tr><td>2</td><td>Helen</td></tr> </tbody> </table></body>'; $this->assertEquals($expected, $result); }
/** * @param string | \Zend\View\Model\ViewModel | Mime\Part $body * @return $this */ public function setBody($body, $charset = null) { $mimeMessage = new Mime\Message(); $finalBody = null; if (is_string($body)) { // Create a Mime\Part and wrap it into a Mime\Message $mimePart = new Mime\Part($body); $mimePart->type = $body != strip_tags($body) ? Mime\Mime::TYPE_HTML : Mime\Mime::TYPE_TEXT; $mimePart->charset = $charset ?: self::DEFAULT_CHARSET; $mimeMessage->setParts([$mimePart]); $finalBody = $mimeMessage; } elseif ($body instanceof Mime\Part) { // Overwrite the charset if the Part object if provided if (isset($charset)) { $body->charset = $charset; } // The body is a Mime\Part. Wrap it into a Mime\Message $mimeMessage->setParts([$body]); $finalBody = $mimeMessage; } elseif ($body instanceof ViewModel) { $view = new View(); $view->setResponse(new Response()); $view->getEventManager()->attach(new PhpRendererStrategy($this->renderer)); $view->render($body); $content = $view->getResponse()->getContent(); $mimePart = new Mime\Part($content); $mimePart->type = Mime\Mime::TYPE_HTML; $mimePart->charset = $charset ?: self::DEFAULT_CHARSET; $mimeMessage->setParts([$mimePart]); $finalBody = $mimeMessage; } else { throw new InvalidArgumentException(sprintf('Provided body is not valid. It should be one of "%s". %s provided', implode('", "', ['string', 'Zend\\Mime\\Part', 'Zend\\Mime\\Message', 'Zend\\View\\Model\\ViewModel']), is_object($body) ? get_class($body) : gettype($body))); } // The headers Content-type and Content-transfer-encoding are duplicated every time the body is set. // Removing them before setting the body prevents this error $this->message->getHeaders()->removeHeader('contenttype'); $this->message->getHeaders()->removeHeader('contenttransferencoding'); $this->message->setBody($finalBody); return $this; }
public function testRender() { $renderer = new PhpRenderer(); $resolver = new Resolver\AggregateResolver(); $map = new Resolver\TemplateMapResolver(array('page' => __DIR__ . '/view/page.phtml', 'comments-list' => __DIR__ . '/view/comments-list.phtml', 'comment' => __DIR__ . '/view/comment.phtml', 'user' => __DIR__ . '/view/user.phtml')); $stack = new Resolver\TemplatePathStack(array('script_paths' => array(__DIR__ . '/view'))); $resolver->attach($map)->attach($stack)->attach(new Resolver\RelativeFallbackResolver($map))->attach(new Resolver\RelativeFallbackResolver($stack)); $renderer->setResolver($resolver); $view = new View(); $response = new Response(); $view->setResponse($response); $strategy = new PhpRendererStrategy($renderer); $strategy->attach($view->getEventManager()); ///////////////////////////////////////////////// $viewConfigExample = ['some-view' => ['template' => 'some-template', 'viewModel' => 'some-view-model|\\Some\\ViewModel::class', 'extend' => 'parent-view', 'capture' => 'some-capture', 'children' => ['child-view'], 'childrenDynamicLists' => ['child-view' => 'listVar'], 'data' => ['fromGlobal' => 'varName', 'fromParent' => 'varName', 'static' => ['key' => 'value']], 'dataProvider' => 'some-view-model|\\Some\\DataProvider::class']]; ////////////// $viewConfig = ['page' => ['template' => 'page', 'children' => ['comments-list', 'comment-create' => ['template' => 'comment-create', 'children' => ['myself-info' => ['viewModel' => \Sebaks\ViewTest\MyselfViewModel::class, 'template' => 'user'], 'comment-create-form' => ['template' => 'form', 'children' => ['form-element-textarea' => ['capture' => 'form-element', 'template' => 'form-element-textarea'], 'form-element-button' => ['capture' => 'form-element', 'template' => 'form-element-button']]]]], 'users-table' => ['template' => 'table', 'children' => ['table-head-rows' => ['template' => 'table-tr', 'data' => ['fromParent' => 'rows'], 'children' => ['table-th' => ['template' => 'table-th', 'capture' => 'table-td', 'data' => ['fromParent' => 'value']]], 'childrenDynamicLists' => ['table-th' => 'rows']], 'table-body-rows' => ['template' => 'table-tr', 'data' => ['fromParent' => 'rows'], 'children' => ['table-td' => ['template' => 'table-td', 'data' => ['fromParent' => 'value']]], 'childrenDynamicLists' => ['table-td' => 'rows']]], 'childrenDynamicLists' => ['table-body-rows' => 'bodyRows', 'table-head-rows' => 'headRows'], 'data' => ['static' => ['headRows' => [['Id', 'Name']], 'bodyRows' => [['1', 'John'], ['2', 'Helen']]]]]]], 'comments-list' => ['template' => 'comments-list', 'children' => ['comment' => ['viewModel' => \Sebaks\ViewTest\CommentViewModel::class, 'template' => 'comment', 'children' => ['user' => ['viewModel' => \Sebaks\ViewTest\UserViewModel::class, 'template' => 'user', 'data' => ['fromParent' => ['comment:userId' => 'userId'], 'static' => ['class' => 'user']]]], 'data' => ['fromParent' => ['comment' => 'comment']]]], 'childrenDynamicLists' => ['comment' => 'comments'], 'data' => ['fromGlobal' => 'comments']]]; $data = ['comments' => [['id' => 'c1', 'userId' => 'u1', 'text' => 'text of c1'], ['id' => 'c2', 'userId' => 'u2', 'text' => 'text of c2']]]; $serviceLocator = new \Zend\ServiceManager\ServiceManager(); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\CommentViewModel::class, \Sebaks\ViewTest\CommentViewModel::class, false); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\UserViewModel::class, \Sebaks\ViewTest\UserViewModel::class, false); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\MyselfViewModel::class, \Sebaks\ViewTest\MyselfViewModel::class, false); ///////////////////// $config = new Config($viewConfig); $viewBuilder = new ViewBuilder($config, $serviceLocator); $pageViewModel = $viewBuilder->build($viewConfig['page'], $data); ///////////////////// $view->render($pageViewModel); $result = $response->getBody(); $expected = '<ul><li>text of c1 <div class="user">John</div></li><li>text of c2 <div class="user">Helen</div></li></ul> <div class="">Me</div><form><textarea></textarea><button type="submit">Submit</button></form><table> <thead> <tr><th>Id</th><th>Name</th></tr> </thead> <tbody> <tr><td>1</td><td>John</td></tr><tr><td>2</td><td>Helen</td></tr> </tbody> </table>'; $this->assertEquals($expected, $result); }