zendframework/zf2 — pull request #4164 by juriansluiman (2013-04-01 19:45:28+00:00)
Title: Fetch model from event parameter

The model used for rendering can be modified during the EVENT_RENDERER and EVENT_RENDERER events. If only attributes (like the template) are modified, the current behaviour does not raise problems. However, when the entire view model is swapped out, the current behaviour does raise a problem.

### Use Case
A use case for swapping out view models is the principle of a three step view pattern. The ``, `

` and contents of the `

` is usually the same for every layout you use. If you want to abstract that contents into a "wrapper" layout, you create a new view model, grab the model from the event and use the model from the event as a child. In code:

```
$model = $e->getModel();

$wrapper = new ViewModel;
$wrapper->setTemplate('layout/bar');
$wrapper->addChild($model);

$e->setModel($wrapper);
```

An example wrapper:

```
doctype() ?>

headTitle() ?>
headMeta() ?>
headLink() ?>
headScript() ?>

content ?>
inlineScript() ?>

```

The layout becomes then simpler:

```
headTitle('My Website')
->setSeparator(' · ')
->setAutoEscape(false);

$this->headLink()
->appendStylesheet($this->basePath() . '/styles/css/styles.css');

$this->inlineScript()
->prependFile('https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
?>

navigation()->menu() ?>

content ?>

Copyright 2013 ACME

```

### This PR
You simply cannot accomplish this or perform any other logic with the view model when the new model is not taken from the event. The simple addition makes it all possible :-)