-
Notifications
You must be signed in to change notification settings - Fork 90
ZendViewRenderer: cannot set layout from within a .phtml template script #68
Comments
There are a few ways to resolve this. First, and easiest: set a default layout in your configuration: return [
/* ... */
'templates' => [
/* ... */
'layout' => 'layout::default',
],
]; Doing so ensures that the layout view model is injected in the renderer at instantiation. The next approach is to provide a layout at the time you call Finally, within your template, you can inject a root view model manually prior to calling use Zend\View\Model\ViewModel;
$this->viewModel()->setRoot(new ViewModel());
$this->layout(/* ... */;
// OR, in one go:
$this->viewModel()->setRoot(
(new ViewModel())->etTemplate(/* layout you want to use */)
); |
Yes, outside the template these are clearly working. Obviously this is not a blocking issue, but would be very elegant to be able to use it just like with the regular ZF2 MVC. |
@djozsef Ah, right; once you've called
The takeaway is: you must have a layout in place before you call the helper in the template. Otherwise, it will raise an exception, as you've already noted. Now, for the long explanation. The reason is because once we pass off rendering to the In the case you've indicated, no layout was provided. This means that the current view model is the root, and when you call Again, the takeaway: set a default layout in your configuration or provide a layout when you render. Believe it or not, the only difference between this and the zend-mvc workflow is that in Expressive, we're not providing a default layout view model. The reason is simple: if we make that the default, then there's no way to render layout-less templates. A number of developers were quite interested in being able to do exactly that (e.g., to deliver rendered HTML snippets over XHR), which led to this implementation. |
@weierophinney Thanks for summing up the full story. Given a deeper thought this is absolutely fine like this. Maybe we should include it in the docs. |
Sotty, i try the expressive now, and this error occurred for me, but your informations and the readdocs not are clear for my, where make this modification? which files? |
@IgorDePaula Not sure if it helps you but the docs have moved to their final location at https://zendframework.github.io/zend-expressive/features/template/zend-view/#layouts |
@IgorDePaula You cannot set the layout from within a template when using ZendViewRenderer. You have to pass the layout's name to the public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
{
$viewData = array(
"layout" => "templates/app/layout.phtml",
"title" => "Hello World Title",
"text" => "Lorem ipsum dolor sit amet ..."
);
$html = $renderer->render("templates/app/home.phtml", $viewData)
return new HtmlResponse($html);
} |
I written this form, but thw getting start recommend the sue of layout view
|
I do all on this page https://zendframework.github.io/zend-expressive/getting-started/skeleton and one hour I received error for getRoot not found for layout, and another hour I received error that view helper e is not registred. |
I got now. I needed disable many settings. |
When Zend\View\Helper\Layout helper called from inside a .phtml scope throws "getRoot: no view model currently registered as root in renderer". Error is thrown at "../vendor/zendframework/zend-view/src/Helper/Layout.php:64" because root is not set so setTemplate() fails.
REPRO:
put this line into any non-layout template script:
Obviously the layout path/map has to be set in config/autoload/templates.global.php
RESEARCH DONE:
The root ViewModel present is null when the helper tries to set template thus it fails. I suspect, that the root cause is that the ViewModel is fetched through DIC/ServiceManager, and at this point it it is already cached with null root. It was probably created in an other run.
I have no idea how to resolve this, sorry.
Could anybody with broader conceptual overview check this?
Thanks.
The text was updated successfully, but these errors were encountered: