@@ -59,24 +59,80 @@ $templates = new ZendView($zendView);
5959## Layouts
6060
6161Unlike the other supported template engines, zend-view does not support layouts
62- out-of-the-box.
62+ out-of-the-box. Expressive abstracts this fact away, providing two facilities
63+ for doing so:
6364
64- Layouts are accomplished in one of two ways:
65+ - You may pass a layout template name or ` Zend\View\Model\ModelInterface `
66+ instance representing the layout as the second argument to the constructor.
67+ - You may pass a "layout" parameter during rendering, with a value of either a
68+ layout template name or a ` Zend\View\Model\ModelInterface `
69+ instance representing the layout. Passing a layout this way will override any
70+ layout provided to the constructor.
6571
66- - Multiple rendering passes:
72+ In each case, the zend-view implementation will do a depth-first, recursive
73+ render in order to provide content within the selected layout.
6774
68- ``` php
69- $content = $templates->render('blog/entry', [ 'entry' => $entry ]);
70- $layout = $templates->render('layout/layout', [ 'content' => $content ]);
71- ```
75+ ### Layout name passed to constructor
7276
73- - View models. To accomplish this, you will compose a view model for the
74- content, and pass it as a value to the layout:
77+ ``` php
78+ use Zend\Expressive\Template\ZendView;
79+
80+ // Create the engine instance with a layout name:
81+ $zendView = new PhpRenderer(null, 'layout');
82+ ```
83+
84+ ### Layout view model passed to constructor
85+
86+ ``` php
87+ use Zend\Expressive\Template\ZendView;
88+ use Zend\View\Model\ViewModel
89+
90+ // Create the layout view model:
91+ $layout = new ViewModel([
92+ 'encoding' => 'utf-8',
93+ 'cssPath' => '/css/prod/',
94+ ]);
95+ $layout->setTemplate('layout');
96+
97+ // Create the engine instance with the layout:
98+ $zendView = new PhpRenderer(null, $layout);
99+ ```
100+
101+ ### Provide a layout name when rendering
102+
103+ ``` php
104+ $content = $templates->render('blog/entry', [
105+ 'layout' => 'blog',
106+ 'entry' => $entry,
107+ ]);
108+ ```
109+
110+ ### Provide a layout view model when rendering
111+
112+ ``` php
113+ use Zend\View\Model\ViewModel
114+
115+ // Create the layout view model:
116+ $layout = new ViewModel([
117+ 'encoding' => 'utf-8',
118+ 'cssPath' => '/css/blog/',
119+ ]);
120+ $layout->setTemplate('layout');
121+
122+ $content = $templates->render('blog/entry', [
123+ 'layout' => $layout,
124+ 'entry' => $entry,
125+ ]);
126+ ```
127+
128+ ## Recommendations
129+
130+ We recommend the following practices when using the zend-view adapter:
75131
76- ```php
77- use Zend\View\Model\ViewModel;
78-
79- $viewModel = new ViewModel(['entry' => $entry]);
80- $viewModel->setTemplate('blog/entry');
81- $layout = $templates->render('layout/layout', [ 'content' => $viewModel ]);
82- ```
132+ - If using a layout, create a factory to return the layout view model as a
133+ service; this allows you to inject it into middleware and add variables to it.
134+ - While we support passing the layout as a rendering parameter, be aware that if
135+ you change engines, this may not be supported.
136+ - While you can use alternate resolvers, not all of them will work with the
137+ ` addPath() ` implementation. As such, we recommend setting up resolvers and
138+ paths only during creation of the template adapter.
0 commit comments