This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 193
Abstract layout capabilities in zend-view adapter #96
Merged
weierophinney
merged 3 commits into
zendframework:master
from
weierophinney:feature/zend-view-layouts
Aug 26, 2015
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,24 +59,80 @@ $templates = new ZendView($zendView); | |
| ## Layouts | ||
|
|
||
| Unlike the other supported template engines, zend-view does not support layouts | ||
| out-of-the-box. | ||
| out-of-the-box. Expressive abstracts this fact away, providing two facilities | ||
| for doing so: | ||
|
|
||
| Layouts are accomplished in one of two ways: | ||
| - You may pass a layout template name or `Zend\View\Model\ModelInterface` | ||
| instance representing the layout as the second argument to the constructor. | ||
| - You may pass a "layout" parameter during rendering, with a value of either a | ||
| layout template name or a `Zend\View\Model\ModelInterface` | ||
| instance representing the layout. Passing a layout this way will override any | ||
| layout provided to the constructor. | ||
|
|
||
| - Multiple rendering passes: | ||
| In each case, the zend-view implementation will do a depth-first, recursive | ||
| render in order to provide content within the selected layout. | ||
|
|
||
| ```php | ||
| $content = $templates->render('blog/entry', [ 'entry' => $entry ]); | ||
| $layout = $templates->render('layout/layout', [ 'content' => $content ]); | ||
| ``` | ||
| ### Layout name passed to constructor | ||
|
|
||
| - View models. To accomplish this, you will compose a view model for the | ||
| content, and pass it as a value to the layout: | ||
| ```php | ||
| use Zend\Expressive\Template\ZendView; | ||
|
|
||
| // Create the engine instance with a layout name: | ||
| $zendView = new PhpRenderer(null, 'layout'); | ||
| ``` | ||
|
|
||
| ### Layout view model passed to constructor | ||
|
|
||
| ```php | ||
| use Zend\Expressive\Template\ZendView; | ||
| use Zend\View\Model\ViewModel | ||
|
|
||
| // Create the layout view model: | ||
| $layout = new ViewModel([ | ||
| 'encoding' => 'utf-8', | ||
| 'cssPath' => '/css/prod/', | ||
| ]); | ||
| $layout->setTemplate('layout'); | ||
|
|
||
| // Create the engine instance with the layout: | ||
| $zendView = new PhpRenderer(null, $layout); | ||
| ``` | ||
|
|
||
| ### Provide a layout name when rendering | ||
|
|
||
| ```php | ||
| $content = $templates->render('blog/entry', [ | ||
| 'layout' => 'blog', | ||
| 'entry' => $entry, | ||
| ]); | ||
| ``` | ||
|
|
||
| ### Provide a layout view model when rendering | ||
|
|
||
| ```php | ||
| use Zend\View\Model\ViewModel | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing |
||
|
|
||
| // Create the layout view model: | ||
| $layout = new ViewModel([ | ||
| 'encoding' => 'utf-8', | ||
| 'cssPath' => '/css/blog/', | ||
| ]); | ||
| $layout->setTemplate('layout'); | ||
|
|
||
| $content = $templates->render('blog/entry', [ | ||
| 'layout' => $layout, | ||
| 'entry' => $entry, | ||
| ]); | ||
| ``` | ||
|
|
||
| ## Recommendations | ||
|
|
||
| We recommend the following practices when using the zend-view adapter: | ||
|
|
||
| ```php | ||
| use Zend\View\Model\ViewModel; | ||
| $viewModel = new ViewModel(['entry' => $entry]); | ||
| $viewModel->setTemplate('blog/entry'); | ||
| $layout = $templates->render('layout/layout', [ 'content' => $viewModel ]); | ||
| ``` | ||
| - If using a layout, create a factory to return the layout view model as a | ||
| service; this allows you to inject it into middleware and add variables to it. | ||
| - While we support passing the layout as a rendering parameter, be aware that if | ||
| you change engines, this may not be supported. | ||
| - While you can use alternate resolvers, not all of them will work with the | ||
| `addPath()` implementation. As such, we recommend setting up resolvers and | ||
| paths only during creation of the template adapter. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
| /** | ||
| * Zend Framework (http://framework.zend.com/) | ||
| * | ||
| * @see http://github.com/zendframework/zend-expressive for the canonical source repository | ||
| * @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
| * @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License | ||
| */ | ||
|
|
||
| namespace Zend\Expressive\Exception; | ||
|
|
||
| use DomainException; | ||
|
|
||
| class RenderingException extends DomainException implements ExceptionInterface | ||
| { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>Layout Page</title> | ||
| </head> | ||
| <body> | ||
| <?= $this->content ?> | ||
| </body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <title>ALTERNATE LAYOUT PAGE</title> | ||
| </head> | ||
| <body> | ||
| <?= $this->content ?> | ||
| </body> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing
;